Convert String To Date PHP
Last Updated : Feb 2, 2023
IN - PHP
In this article we will show you the solution of convert string to date PHP, here to achieve desired result we need to use strtotime() method in php. The strtotime() function used to parses an English textual datetime into unix timestamp format.
To this method you can provide various types of input that returns to you timestamp according to your input. This is just single line code, which leads you to get timestamp various types of output.
Step By Step Guide On Convert String To Date PHP :-
First you should define echo statement, then specify strtotime() method with several types of input to helps user to know more functions.
Through this program, we are shown 5 different types of inputs with appropriate output function.
These are covered overall possible function with output that can helps later at any stage. Before execution make sure server whether started or not if you forget then you cannot see output.
<?php echo (strtotime("next Tuesday"). "<br>"); echo (strtotime("now"). "<br>"); echo (strtotime("+1 week 2 days 4 hours"). "<br>"); echo strtotime("+1 week"); ?>
- A php script can be placed anywhere in the document. A php script starts with <?php and end with ?>.
- The default file extension for php files is “.php” and php statements end with ‘;’ semicolon. In php we need to specify ‘$’ symbol before the variable name also default rule.
- We defined echo with strtotime() method various inputs such as ‘next Tuesday, now, +1 week 2 days 4 hours, +1 week’.
- Each strtotime() method near we concatenated <br> tag to break the line of output at each time so you gets neat output. You can replace this with ‘\n’ statement it also acts same as html break tag.
- Those various inputs given in string format like our usual language and you getting output in unix datetimestamp format. We can also change this format of obtained result into clear date time format through date() method. One more thing is provided proper input with overall possible otherwise you cannot achieve result.
- Date() generally helps everyone to modify date time format as per requirement. Passing strtotime() method returned value to date() method deliver you perfect or our usual date and time format result unlike these numeric digit format.
- Instead echo() you can prefer print() method if you want and that not going to affects your result. Still some type of input exist you can try those along with it to master your mind in this concept.
In conclusion, now we are able know how to convert string to date of unix format in php.
When work with php we need to create and process php files at server location and then we need to start the server before execute the program.
When we execute this program on browser it will print ‘1672700400, 1672500186, 1673292186, 1673104986’ at webpage browser.
I hope this article on convert string to date PHP helps you and the steps and method mentioned above are easy to follow and implement.