Converting string to date in PHP

Converting strings to dates in PHP requires the use of two functions. strtotime function converts the string to an epoch timestamp. date function converts the timestamp to the desirable date format.

<?php
   $string = "Nov 11, 2011";
   $timestamp = strtotime($string);
   $date = date("Y-m-d", $timestamp);
   print $timestamp . "<br>" . $date;
?>

output

1320987600
2011-11-11

Y prints the date in 4 digit format, m prints the month in two digit format, and d prints the day in two digit format. Alternately, you can use the single line version of the code:

print date("Y-m-d", strtotime("Nov 11, 2011"));
Technologies: