Convert a date string to PHP format - strtotime()
During the development of a PHP calendar, I needed to convert a string to a PHP date, let's assume the following string:"2015/01/29"I wanted to convert this date to 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 date string in a PHP:
phpThe structure of the $date variable will be displayed like this:
// (1) returns a Unix timestamp, such as 1422486000
$time = strtotime("2011/05/21");
// (2) getDate() returns an array that contains the
// information of that timestamp, or the local time if no
//timestamp is given
$date = getDate($time);
print_r($date);
?>
Arrayit works and even if it's not the best structure you can get, it's at least possible to get the basic information about the day, month, and current year.
(
[seconds] => 0
[minutes] => 0
[hours] => 0
[mday] => 29
[wday] => 4
[my] => 1< br /> [year] => 2015
[yday] => 28
[weekday] => Thursday
[month] => January
[0] => 1422486000
)
Resources:
Converting string to Date and DateTime