Question

$datetime = new DateTime('0000-00-00 00:00:00');
$date_string = $datetime->format('Y-m-d H:i:s');//-0001-11-30 00:00:00 

date gets turned from 0000-00-00 00:00:00 to -0001-11-30 00:00:00 this is obviously wrong, why does this happen, how to fix it? Should at least return false.

info: PHP Version 5.2.13-0.dotdeb.1 Linux 2.6.26-2-openvz-amd64 #1 SMP Thu Nov 25 05:14:47 UTC 2010 x86_64

Was it helpful?

Solution

It's sort of correct by definition:

  • the zero-th day as opposed to the first yields a (hypothetical) minus one day
  • the zero-th month as opposed to the first yields a (hypothetical) minus one month

Take the (hypothetical) 1st of Jan in the year 0, subtract a month -> 1st of Dec in the year -1. Subtract a day -> 30th of Nov in the year -1

Alternatively, they could have chosen to fix up the day first, then it'd go like: 1 Jan minus 1d -> 31st of Dec -0001, minus 1mo -> 30th of Nov -0001

The formatter obviously does the bound check to only produce valid dates. (For some definition of ``valid'')

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top