Adding years to a date resets to 1970-01-01

134
February 28, 2019, at 9:00 PM
$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?

Answer 1

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');
Answer 2

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");
Answer 3

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.

Answer 4

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

Answer 5

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.

Rent Charter Buses Company
READ ALSO
How can I attach a support case to a purchase order using Suitetalk?

How can I attach a support case to a purchase order using Suitetalk?

When we are on a purchase order in the UI and hit 'Create New Case' it creates a case that is attached to the purchase orderWe would like the cases we create through Suitetalk to also get attached to the purchase order

234
cors origin restriction wordpress woocommerce

cors origin restriction wordpress woocommerce

I have a wordpress website with woocommerce plugin and I'm trying to load that website into a mobile application with ionic4 framework, my app work with no problems on andriod but I got this error when I run my app on iOS device

171
Pass all HTML table values to PHP to store them in database using Javascript

Pass all HTML table values to PHP to store them in database using Javascript

I have an HTML table which I populate from database using a buttonI want to get all the values from the table and send them to a php file to store each row in the database using Javascript when a button is clicked

158
How to access local web server on Mac

How to access local web server on Mac

Using the build-in apache and mysql, I created a database using mysql in Terminal and then I imported a php file, also using TerminalThe question is how to I access this website now in Safari? With which ip or hostname? Using show variables; indicates...

190