The strtotime()
function can only convert a limited number of formats, as detailed in the Supported Date and Time Formats section in the PHP manual.
The following note from the Date Formats page is pertinent in this case as the 2018
part of your example is interpreted as a 24-hour time value.
Note:
The "Year (and just the year)" format only works if a time string has already been found -- otherwise this format is recognised as HH MM.
You could use string manipulation (e.g. use preg_replace()
or str_replace()
to remove the ,
) to provide a date format that PHP interprets as you want. I would prefer using a regex, eg:
$sd = "20th Dec, 2016";
echo date("y", strtotime(preg_replace("/([\w ]+),([\w ]+)/", "$1$2", $sd)));
A good alternative would be to use DateTime::createFromFormat()
, with which you state exactly the format that the date string has.
$sd = "20th Dec, 2016";
$dt = DateTime::createFromFormat("dS M, Y", $sd);
echo $dt->format("d-M-Y");
echo strtotime($dt->format("d-M-Y"));