Why is this odd behaviour ?
I want to create this date 2009-02-15 to my specified format, but it shows a year of 1970 . Why is this happening ? I found this example on PHP.net manual.
<?php
$format = 'Y-m-!d H:i:s';
$date = DateTime::createFromFormat($format, '2009-02-15 15:16:17');
echo "Format: $format; " . $date->format('Y-m-d H:i:s') . "
"; //Output is Format: Y-m-!d H:i:s; 1970-01-15 15:16:17
That's because of the !
. From the manual for DateTime::createFromFormat
:
If format contains the character
!
, then portions of the generated time not provided in format, as well as values to the left-hand side of the!
, will be set to corresponding values from the Unix epoch.
This means that anything left of the !
will be set to 1970-01-01 00:00:00 UTC
. In your case (Y-m-!d H:i:s
) that means that the year and month will be set to January 1970, while the remaining parts of the date will be set according to your string.
Why is it !
sign in precise format? It should not be there. Correct format is Y-m-d H:i:s
, so:
$format = 'Y-m-d H:i:s';
$date = DateTime::createFromFormat($format, '2009-02-15 15:16:17');
-see manual page, it explains meaning of !
(and you need not have it in precise format)
Check It
$date = date('Y-m-d H:i:s',strtotime('2009-02-15 15:16:17'));
echo $date;