php datetime object 1960 year limitation
536
I am trying to use php datetime object for handling dates.
Here is my code:
$date = new DateTime('01 Dec, 1969');
echo $date->format('Y-m-d');
The above code returns2010-12-01
But If I change year from 1969 to 1945 or anything less than 1960 then the code returns incorrect year. For example:
This code:
$date = new DateTime('01 Dec, 1950');
echo $date->format('Y-m-d');
returns 2010-12-01
Answer
Solution:
This is likely a bug. Consider filing it to the bugtracker.
When you change the input format to
PHP will correctly make this into
See http://codepad.org/trFfB6Q1
As of PHP5.3, you can also use
to create a date. This would work with your original DateTime string then:
See http://codepad.viper-7.com/08kK5M
Answer
Solution:
Given that this problem does not occur on my system (PHP5.3 on a windows machine) I suggest you update to php 5.3. There are no drawbacks and this is probably not the only bug you will run into. I have tested different date formats('1969/12/1','01 Dec, 1969',..) and had no problems at all. if the problem persist feel free to slap me ;)
Answer
Solution:
PHP's Datetime is based on a unix timestamp which started counting from 1st of january 1970.
You cannot use DateTime to acces a date before that.