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

194

Answer

Solution:

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

980

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

594

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.

People are also looking for solutions to the problem: php - Does parent:: automatically determine class and object invoke context?

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.