17

I am of of the understanding that due to the nature that PHP represents dates using milliseconds, you cannot represent dates past 2038. I have a problem where I want to calculate dates far in the future. Thousands of years away.

Obviously I cannot use the php date function to represent this date because of the limit, however, I have something on my side... All I want to do is store the year, month and day. I do not care for the hour, minute, seconds and milliseconds.

Am I correct in thinking that without including this additional information I should be able to calculate a lot further into the future because I am willing to discard a lot of information. Is this any library that currently does this? If not does any have any advice on how to approach this problem?

O. Jones
  • 103,626
  • 17
  • 118
  • 172
Moz
  • 1,494
  • 6
  • 21
  • 32
  • Using timestamps is only a problem on 32-bit setups. PHP running on 64-bit machines can represent dates beyond 2038. – mario Mar 16 '11 at 00:38
  • 1
    As if PHP 5.6.15 64-bit Windows build uses 32-bit timestamps. Also, the strtotime() function returns 32-bit signed timestamp (returns negative values for dates after 2038) and the date() function understands only 32-bit signed timestamps. – Alexander Pravdin Nov 27 '15 at 05:50
  • I am using phpmyadmin and trying to insert 2038 datetime in MySQL but I get an error, no idea why that's happening. I am running on 64bit system. @KevinEvans any ideas? – Petar Vasilev Jun 20 '19 at 18:38

5 Answers5

26

You can alternatively use the DateTime class, which internally represents the time components independently. Thus it is not susceptible to the 2038 limitation (unless you use ::getTimestamp).

mario
  • 144,265
  • 20
  • 237
  • 291
  • Thank you it looks like this is going to work for me because I will not need to use any timestamps, superb :) – Moz Mar 16 '11 at 00:50
  • Is there a way to get the timestamp back out without it being truncated? – MrMesees Sep 26 '17 at 13:15
  • This is only a very limited solution! What if I want to use filemtime() to get the date of a file after 2038 ? – Elmue Nov 17 '17 at 19:38
  • 1
    @Elmue `filemtime()` uses the underlying timespec struct/integer size of the OS. In the case of 64-bit OSes, it returns dates larger than 2038. – mario Nov 17 '17 at 21:59
  • 1
    Completely correct. But if I have to use a 64 bit OS I don't need your answer anymore. The DateTime class is useless if I want to work with file times. So the correct answer is the one from alex: If you want to use dates after 2038 you MUST use a 64 bit OS. The DateTime class is only a very limited workaround. This is just what I wrote in my comment! – Elmue Nov 17 '17 at 22:11
  • The x64 builds of PHP 7 support native 64-bit integers, LFS, 64-bit memory_limit and much more. – kewlashu Jul 31 '20 at 10:40
9

You could use a 64bit platform.

The size of an integer is platform-dependent, although a maximum value of about two billion is the usual value (that's 32 bits signed). 64-bit platforms usually have a maximum value of about 9E18.

Source.

Find out your platform is 64bit with var_dump(PHP_INT_SIZE === 8). If TRUE, your system is 64 bit.

alex
  • 479,566
  • 201
  • 878
  • 984
  • 1
    Another option to test this is: echo 0x7FFFFFFFFFFFFFFF; which should print 9223372036854775807 if integer is 64 bit. – Elmue Nov 17 '17 at 19:33
  • 1
    Nevertheless what a shame is PHP! Why are the PHP folks not able to compile a 32 bit version that uses variables of 64 bit? This can be done with a few compiler switches. – Elmue Nov 17 '17 at 22:13
2

PHP has introduced Datetime() class in version 5.2 to solve this problem. But you still must be in 64-bit OS.

Haza
  • 2,991
  • 1
  • 16
  • 13
  • 1
    If this is true, then PHP is completely misdesigned. PHP internally is programmed in C++ which easily allows to use 64 bit variables EVEN on a 32 bit platform! (see ULONGLONG) – Elmue Nov 17 '17 at 19:34
1

Forget Windows! Even if you're running Apache 64-Bit over Windows 64-Bit!

$timestamp = strtotime('22-09-2508');
var_dump ($timestamp);
// returns bool false using WAMP 64-Bit over Windows 64 Bit

If you need to calculate timestamps, use a 64 bit system (not Windows):

$timestamp = strtotime('22-09-2508');
var_dump ($timestamp);
// returns int 17000496000 using a LAMP Server

You can run a LAMP Server using VMWare over your Windows and most likely your final host server will use this 64-Bit service as well.

In other words:

if (intval("9223372036854775807")==9223372036854775807) {
  // 64-bit
} else {
  // 32-bit
}

Got it ?

Note: time() and getTimestamp() both work ok in 64bit environment (linux) after year 2038 ..

Jack M.
  • 3,676
  • 5
  • 25
  • 36
  • As if PHP 5.6.15 64-bit Windows build uses 32-bit timestamps. Also, the strtotime() function returns 32-bit signed timestamp (returns negative values for dates after 2038) and the date() function understands only 32-bit signed timestamps. – Alexander Pravdin Nov 27 '15 at 05:51
  • 4
    It is COMPLETE nonsense to say "Forget Windows". As if this would depend on the operating system! PHP internally is C++ code. On Windows you can EASILY use 64 bit variables EVEN on a 32 bit platform. Visual Studio header files define 64 bit types as ULONGLONG or LONGLONG since decades. It is the fault of PHP folks who make a LOT of design errors. The correct answer would be: "Forget PHP". It is a shame that PHP still uses 32 bit signed integers. Why does PHP not offer 64 bit variables from THE FIRST DAY, like C++, C# and others do? It is a severe design error in PHP, not in Windows! – Elmue Nov 17 '17 at 19:25
1

You are correct in that PHP doesn't allow you to handle dates > 2038, natively. However there are libraries such as this one that take advantage of the fact that floating points are 64 bit, thus allowing you to bypass this issue if need be. (All under the assumption that you are using a 32 bit system... if you are using 64 bit, you are fine).

Mike Lewis
  • 63,433
  • 20
  • 141
  • 111