Beforehand, I have read this question:
How to prevent PHP to convert a DateTime object with 0000-00-00 value to -0001-11-30
But I dont get why 0000-00-00 00:00:00 changes to -0001-11-30 00:00:00 when I run this code:
$date = date_create('0000-00-00 00:00:00');
echo date_format($date, 'Y-m-d H:i:s');
When I try it with 0001-00-00 00:00:00 I get:
0000-11-30 00:00:00
and with 0001-01-01 00:00:00 I get:
0001-01-01 00:00:00
and with 0000-00-01 00:00:00:
-0001-12-01 00:00:00
Is there any specific reason why it's always a year/day/month before the nonexisting date?
Is there something wrong with the functions date_create or date_format?
I do notice that the time is displayed the right way and that's probably because the time 00:00:00 exists.
It's like @Mark Baker said, 0000-00-00 00:00:00
is an invalid date, because there is no month zero, no day zero.... so it's month 1 (Jan) - 1 (Dec of previous year) and day 1 - 1 (Goes to last day of previous month, giving 30th November).
If you see close enough about this behavior in date_create. It says DateTime
will recognize any number up to 12 as a [month]
, and any number up to 31 as a [day]
; it calculates the resulting date to be [day]
days after the start of [month]
. This means that when a datetime
object is created with more days than are found in that month, the date will be beyond the end of the month. This also applies if the date you're create is invalid date. :)
This is a known issue with DateTime function in php. The datetime function does not have a proper error handling.
Other functions like strtotime does handle it properly.
You can refer this for more reference.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With