26

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.

MacMac
  • 34,294
  • 55
  • 151
  • 222
  • 2
    For MySQL, see also http://stackoverflow.com/questions/2012589/php-mysql-year-2038-bug-what-is-it-how-to-solve-it – Wrikken Oct 17 '10 at 13:30
  • That link also has good background info on the PHP side – Pekka Oct 17 '10 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) – Florian Müller Sep 03 '13 at 12:27

6 Answers6

46

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

Pekka
  • 442,112
  • 142
  • 972
  • 1,088
  • Thanks for this Pekka. Will this ever be solved once we nearly reach to it? – MacMac Oct 17 '10 at 13:54
  • @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 – Pekka Oct 17 '10 at 13:55
  • 1
    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... – Tomasz Kapłoński Oct 21 '15 at 07:10
39

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
Gordon
  • 312,688
  • 75
  • 539
  • 559
  • 1
    Yup, using `DateTime` and - on mySQL side - the `DATETIME` field type fixes the issue completely. – Pekka Oct 17 '10 at 13:33
8

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

oglgo
  • 81
  • 1
  • 4
    Mmm, are you sure? Will PHP's internal handling of integers turn to 64-bit automatically? – Pekka Oct 17 '10 at 15:43
  • I'm not exactly sure, but it seems to roll over on my box... :-) – oglgo Oct 17 '10 at 16:51
  • 4
    64-bit will be okay for the next ~300 billion years. – Jonah Jun 07 '13 at 16:33
  • 1
    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." http://php.net/manual/en/language.types.integer.php – Westy92 Sep 29 '15 at 04:39
  • 1
    Well if I remember correctly, XAMPP on Window still uses 32 bit integers even on 64 bit machines. – Christian Apr 04 '19 at 19:41
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.

Ram Sharma
  • 8,676
  • 7
  • 43
  • 56
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

Ram Sharma
  • 8,676
  • 7
  • 43
  • 56
Vladyslav Savchenko
  • 1,282
  • 13
  • 10
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
sanmai
  • 29,083
  • 12
  • 64
  • 76