Maximum time() | PHP
Solution 1
The last 32-Bit Integer timestamp will be reached January 19, 2038. This is known as the Year 2038 problem.
Solution 2
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
Solution 3
Remember, the Y2038 problem does not apply on 64-bit systems.
Solution 4
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
Solution 5
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
Related videos on Youtube
Comments
-
MacMac about 4 years
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.
-
Wrikken over 13 yearsFor MySQL, see also stackoverflow.com/questions/2012589/…
-
Pekka over 13 yearsThat link also has good background info on the PHP side
-
Florian Müller over 10 yearsHas that been fixed recently in PHP's
time()
? The biggest value it can make to a date is now9223372036854772207
(04.12.292277026596 15:30:07
) (very close to the 64 Bit Integer max value)
-
-
Pekka over 13 yearsYup, using
DateTime
and - on mySQL side - theDATETIME
field type fixes the issue completely. -
MacMac over 13 yearsThanks for this Pekka. Will this ever be solved once we nearly reach to it?
-
Pekka over 13 years@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 over 13 yearsMmm, are you sure? Will PHP's internal handling of integers turn to 64-bit automatically?
-
oglgo over 13 yearsI'm not exactly sure, but it seems to roll over on my box... :-)
-
Jonah almost 11 years64-bit will be okay for the next ~300 billion years.
-
Westy92 over 8 yearsBe 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
-
Tomasz Kapłoński over 8 yearsBasically 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...
-
Christian about 5 yearsWell if I remember correctly, XAMPP on Window still uses 32 bit integers even on 64 bit machines.