In this post you will learn How to Convert a String into Date Format in PHP.
During development of a PHP calendar, I needed to convert a string to a PHP date, suppose the following string:
1 2 3 | "2015/01/29" |
I wanted to convert this date into a format understandable by PHP: 2015-01-29.
The following code shows in two steps the use of the strtotime() and getDate() function to convert a string date into a PHP date structure:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | <?php // (1) returns a Unix timestamp, like 1422486000 $time = strtotime("2011/05/21"); // (2) getDate() returns an array that contains the // information of this timestamp, or local time if no // timestamp is not given $date = getDate($time); echo "<pre>"; //it's not important print_r($date); ?> |
The structure of the $ date variable will be displayed like this:
it works and even if it is not the best structure that can be obtained, it is possible at least to get the basic information on the day, the month, and the current year.