28

It's kind of a silly question, but what would be the maximum INT value of a time() and it's future date, e.g.

1st January 2999

Would time() ever get to that value? Going for a large time() value would return this:

Thu 1st Jan 1970 1:00AM

A normal int date

1287320788 - outputs today's date: Sun 17th Oct 2010 2:06PM

But I'm only curious for the biggest int date and the last date.

3
  • 2
    For MySQL, see also stackoverflow.com/questions/2012589/… Commented Oct 17, 2010 at 13:30
  • That link also has good background info on the PHP side Commented Oct 17, 2010 at 13:32
  • 1
    Has that been fixed recently in PHP's time()? The biggest value it can make to a date is now 9223372036854772207 (04.12.292277026596 15:30:07) (very close to the 64 Bit Integer max value) Commented Sep 3, 2013 at 12:27

6 Answers 6

48

The last 32-Bit Integer timestamp will be reached January 19, 2038. This is known as the Year 2038 problem.

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

3 Comments

Thanks for this Pekka. Will this ever be solved once we nearly reach to it?
@YouBook if PHP still exists by then, probably. It's going to be easy to fix by switching the underlying system to larger INTs - Won't be much of a problem thanks to PHP's weak typing. Alternatively, use DateTime as suggested by Gordon
Basically the problem will solve itself by definition since 2038 is max int in 32-bit system and most computers are already 64-bit which means waaaay more range in terms of integers...
41

PHP stores the highest integer number it can represent in the PHP_INT_MAX constant:

date('Y-m-d H:i:s', PHP_INT_MAX); // 2038-01-19 04:14:07

If you want to work with dates beyond that, consider using the DateTime API, e.g.

$dt = new DateTime('1st January 2999');
$dt->add(DateInterval::createFromDateString('+1 day'));
echo $dt->format('Y-m-d H:i:s'); // 2999-01-02 00:00:00
echo $dt->format('U');           // 32472226800

1 Comment

Yup, using DateTime and - on mySQL side - the DATETIME field type fixes the issue completely.
9

Remember, the Y2038 problem does not apply on 64-bit systems.

5 Comments

Mmm, are you sure? Will PHP's internal handling of integers turn to 64-bit automatically?
I'm not exactly sure, but it seems to roll over on my box... :-)
64-bit will be okay for the next ~300 billion years.
Be careful. The Y2038 problem always applies on Windows systems. "64-bit platforms usually have a maximum value of about 9E18, except for Windows, which is always 32 bit." php.net/manual/en/language.types.integer.php
Well if I remember correctly, XAMPP on Window still uses 32 bit integers even on 64 bit machines.
2

DateTime seems to use 32bit on 64bit servers, too. So you get into trouble.

I've solve it this way:

new DateTime("99999/12/31 00:00:00");

Because, the date overflows the maximum length for DateTime, date use the maximum possibel value and returns a DateTime-object like this (inspected with var_dump) :

object(DateTime)#9 (3) { ["date"]=> string(19) "2031-09-12 00:00:00" ["timezone_type"]=> int(3) ["timezone"]=> string(13) "Europe/Berlin" } }

I'm not sure if it differs with the versions of PHP. I've tested it with version 5.4.

Comments

2

The shortest way I know is to get tomorrow's date:

date("Y-n-j", strtotime("+1 day"))

date("Y-n-j", PHP_INT_MAX) on 64bit-systems gives potential dangerous value: 292277026596-12-4

Comments

2

On 64-bit platforms PHP_INT_MAX does not reflect maximum INT value for 32-bit platforms. Here's how to get it:

$max32bitInt = PHP_INT_SIZE == 4 ? PHP_INT_MAX : PHP_INT_MAX>>32;

If you're always using 64-bit platform, just use:

PHP_INT_MAX>>32

Comments

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.