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:


// (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);

?>
The structure of the $date variable will be displayed like this:

Array
(
[seconds] => 0
[minutes] => 0
[hours] => 0
[mday] => 29
[wday] => 4
[my] => 1< br /> [year] => 2015
[yday] => 28
[weekday] => Thursday
[month] => January
[0] => 1422486000
)
it 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.

Resources:
Converting string to Date and DateTime