3

I need to get year 30+ years in the future, which is why I am using below code.

$currentDate = date("Y-m-d"); 
$dateOneYearAdded = strtotime(date("Y-m-d", strtotime($currentDate)) . " +30 year");
$endYr = date('Y', $dateOneYearAdded);

But as I tested this code its working for 26 years only. I can add only 26 year into the current year. If I try to add more than 26 then it will revert to the year 1970.

Below code is working fine:

$currentDate = date("Y-m-d"); 
$dateOneYearAdded = strtotime(date("Y-m-d", strtotime($currentDate)) . " +26 year");
$endYr = date('Y', $dateOneYearAdded);

Is there something wrong with my code or is it an error in the PHP function?

1
  • 1
    You need to use the datetime object if you want to go past the 2030s Commented Jun 20, 2011 at 6:32

5 Answers 5

5

Congratulations, you have encountered the Y2K38 problem. You will either need to switch to a 64bit system, or use the DateTime class, which can represent (virtually) arbitrary dates.

Sign up to request clarification or add additional context in comments.

Comments

3

See this article for information.

The UNIX timestamp counts the number of seconds since 1970, and as a 32-bit integer it has a maximum range of 2038.

You should use a DateTime object instead.

And of course, if you are storing dates in a database, be sure to use a DATE or DATETIME field.

Comments

1

It's the timestamp you're asking for is greater than the maximum integer value that PHP can hold on your system.

Comments

1

This is so because

The valid range of a timestamp is typically from Fri, 13 Dec 1901 20:45:54 GMT to Tue, 19 Jan 2038 03:14:07 GMT. (These are the dates that correspond to the minimum and maximum values for a 32-bit signed integer). However, before PHP 5.1.0 this range was limited from 01-01-1970 to 19-01-2038 on some systems (e.g. Windows).

you can use DateTime class instead

Comments

0

According to the PHP - date() function changelog,

The valid range of a timestamp is typically from Fri, 13 Dec 1901 20:45:54 GMT to Tue, 19 Jan 2038 03:14:07 GMT. (These are the dates that correspond to the minimum and maximum values for a 32-bit signed integer). However, before PHP 5.1.0 this range was limited from 01-01-1970 to 19-01-2038 on some systems (e.g. Windows).

So be sure to check the DateTime Class

1 Comment

Upgrading PHP won't help if you're still on a 32 bit system - DateTime is the right solution.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.