Why does 00:00:00 0000-00-00 return -0001-11-30 00:00:00?

In advance, I read this question:

How to prevent PHP from converting a DateTime object with a value of 0000-00-00 to a value of -0001-11-30

But I don’t 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, 'Ymd H:i:s'); 

When I try it with 0001-00-00 00:00:00, I get:

 0000-11-30 00:00:00 

and from 0001-01-01 00:00:00 I get:

 0001-01-01 00:00:00 

and from 0000-00-01 00:00:00:

 -0001-12-01 00:00:00 

Is there any specific reason why it always amounts to a year / day / month before the date of absence?

Is there something wrong with the date_create or date_format functions ?

I notice that the time is displayed correctly and probably because there is a time 00:00:00.

+8
date php
source share
3 answers

This is similar to @Mark Baker: 0000-00-00 00:00:00 is an invalid date because there is no zero month, there is no zero day .... so month 1 (Jan.) - 1 (December of the previous year) and day 1 - 1 (Go to the last day of the previous month, dated November 30).

If you are close enough to this behavior in date_create . He says that DateTime will recognize any number up to 12 as [month] , and any number up to 31 as [day] ; it calculates the total date as [day] days after the start of [month] . This means that when a DateTime is created with more days than this month, the date will be outside the end of the month. This also applies if the date you create is an invalid date. :)

+2
source share

This is a known issue with DateTime function in php. The datetime function does not have proper error handling.

Other functions, such as strtotime, handle it correctly.

You can refer to for more information.

0
source share

Maybe your server time zone or time setting.

Try (re) setting the time zone

 <?php // get the existing timezone echo date_default_timezone_get(); // set up the correct one for your location date_default_timezone_set("Europe/Dublin"); // check if new timezone was applied echo date_default_timezone_get(); ?> 

http://php.net/manual/en/function.date-default-timezone-set.php

-2
source share

All Articles