Adding years to a date resets to 1970-01-01
Asked Answered
I

5

4
$somedate = "1980-02-15";
$otherdate = strtotime('+1 year', strtotime($somedate));
echo date('Y-m-d', $otherdate);

outputs

1981-02-15

and

$somedate = "1980-02-15";
$otherdate = strtotime('+2 year', strtotime($somedate));
echo date('Y-m-d', $otherdate); 

outputs

1982-02-15

but

$somedate = "1980-02-15";
$otherdate = strtotime('+75 year', strtotime($somedate));
echo date('Y-m-d', $otherdate); 

outputs

1970-01-01

How to fix?

Intransigeance answered 7/6, 2012 at 2:24 Comment(0)
B
5

It's the 2038 bug which is like y2k where systems can't handle dates after that year due to 32 bit limitations. Use the DateTime class instead which does work around this issue.

For PHP 5.3+

$date = new DateTime('1980-02-15');
$date->add(new DateInterval('P75Y'));
echo $date->format('Y-m-d');

For PHP 5.2

$date = new DateTime('1980-02-15');
$date->modify('+75 year');
echo $date->format('Y-m-d');
Bipack answered 7/6, 2012 at 2:27 Comment(4)
how to add 75 years to a given string of "1980-02-15" using the datetime class?Intransigeance
Thanks. I get Fatal error: Call to undefined function date_add(). I am using PHP Version 5.2.17.Intransigeance
Ahh. This requires 5.3. Lemme see if I can find a 5.2 example.Bipack
Ok, found an alternative for PHP 5.2Bipack
S
4

strtotime() uses a unix timestamp, so it overflows if it attempts to calculate years beyond 2038 and reverts back to 1970.

To get around this, use the DateTime object. http://php.net/manual/en/book.datetime.php

To add a period of time to a DateTime object, use DateTime::add, which takes a DateInterval as a parameter. http://php.net/manual/en/datetime.add.php http://www.php.net/manual/en/class.dateinterval.php

$date = new DateTime("1980-02-15");
if (method_exists("DateTime", "add")) {
    $date->add(new DateInterval("Y75"));
} else {
    $date->modify("+75 years");
}
echo $date->format("Y-m-d");
Seamanship answered 7/6, 2012 at 2:27 Comment(3)
how to add 75 years to a given string of "1980-02-15" using the datetime class?Intransigeance
Thanks. I get Fatal error: Call to undefined method DateTime::add(). I am using PHP Version 5.2.17.Intransigeance
::add is only in 5.3+. You'll want to call ::modify on it with a string parameter alike one you would use for strtotime.Seamanship
W
1

For unix timestamp, the maximum representable time is 2038-01-19. At 03:14:07 UTC.

So you can't represent/operate time that over it by using timestamp.

Weakminded answered 7/6, 2012 at 2:28 Comment(0)
B
1

PHP's dates are limited to a range from 01-01-1970 to 19-01-2038. You will have to use a different method for working with dates.

PEAR has a Date class : PEAR Date

Battleax answered 7/6, 2012 at 2:31 Comment(0)
M
0

75 years from 1980 is 2055, which is past the highest date value that can be represented in a 32-bit integer. Therefore the result becomes 0, which is the 1970 date you observe.

Methuselah answered 7/6, 2012 at 2:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.