17

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.

Community
  • 1
  • 1
Loko
  • 6,539
  • 14
  • 50
  • 78
  • It tries to correct the date you've given it. Similarly to correcting months over 12 by adding 1 to the year, it corrects months below 1 by subtracting 1 from the year. Same for the day. – Mr Lister Apr 28 '15 at 11:07
  • 7
    `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).... and this is documented behaviour.... `Values less than 1 (including negative values) reference the days in the previous month, so 0 is the last day of the previous month, -1 is the day before that, etc. Values greater than the number of days in the relevant month reference the appropriate day in the following month(s).` – Mark Baker Apr 28 '15 at 11:07
  • @MarkBaker Is date_create or date_format doing this and do you perhaps know why it's doing this? – Loko Apr 28 '15 at 11:10

3 Answers3

6

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. :)

Eko Junaidi Salam
  • 1,663
  • 1
  • 18
  • 26
  • I do know that `0000-00-00 00:00:00` is invalid. Just saying. Though the note about the comment I didn't see is nice although it doesn't explain why it does this. – Loko Apr 28 '15 at 11:19
  • This behavior seems just like the manual said and just like url [reference](http://stackoverflow.com/questions/10450644/how-do-you-explain-the-result-for-a-new-datetime0000-00-00-000000) @chandres_cool answer :) – Eko Junaidi Salam Apr 28 '15 at 11:23
  • Technically, there is no year zero either. https://en.wikipedia.org/wiki/Year_zero – Rahly Dec 22 '22 at 04:30
1

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.

Community
  • 1
  • 1
chandresh_cool
  • 11,753
  • 3
  • 30
  • 45
  • It's debatable whether this is an "issue" and "error" or a documented behaviour. (I know, cliché feature-not-a-bug response...) – deceze Apr 28 '15 at 12:15
  • @Loko In a nutshell: it's doing time math. More than 12 months will roll over into the next year, fewer than 1 will roll back into the previous year and so on... `mktime` does the same, and it's decidedly a feature. – deceze Apr 28 '15 at 12:29
  • @deceze Sorry I pronounced the comment wrong. I dont seem to get why they do it like that. Not that you have to answer this though. – Loko Apr 28 '15 at 12:31
  • 1
    @Loko We can't speculate as to the intentions of the original implementer. *Someone* thought it's useful at some point. Arguably it is sometimes. Arguably PHP also didn't always implement the best ideas... – deceze Apr 28 '15 at 12:33
-2

Could be your server timezone, or a time adjustment.

Try (re)setting the timezone

<?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

Ciprian
  • 872
  • 1
  • 10
  • 30
  • 1
    Nope doesn't do anything. NOTE: Whatever I'm getting, is normal behaviour in PHP apparently. I'm asking why it is behaving like this. – Loko Apr 28 '15 at 11:08
  • What about changing your "`0000-00-00 00:00:00`" to "`0000-00-00 00:00:00 GMT`" in the `date_create()` function? – Ciprian Apr 28 '15 at 11:11