开发者

php datetime object 1960 year limitation

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 returns 2010-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 20开发者_JAVA技巧10-12-01


This is likely a bug. Consider filing it to the bugtracker.

When you change the input format to

$date = new DateTime('Dec 1st, 1950');
echo $date->format('Y-m-d');

PHP will correctly make this into

1950-12-01

See http://codepad.org/trFfB6Q1


As of PHP5.3, you can also use DateTime::createFromFormat to create a date. This would work with your original DateTime string then:

$date = DateTime::createFromFormat('d M, Y', '01 Dec, 1950');
echo $date->format('Y-m-d');

See http://codepad.viper-7.com/08kK5M


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 ;)


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜