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?

2
  • Using timestamps is only a problem on 32-bit setups. PHP running on 64-bit machines can represent dates beyond 2038.
    – mario
    Mar 16, 2011 at 0: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. Nov 27, 2015 at 5:50

6 Answers 6

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).

6
  • 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, 2011 at 0:50
  • Is there a way to get the timestamp back out without it being truncated?
    – MrMesees
    Sep 26, 2017 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, 2017 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, 2017 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, 2017 at 22:11
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.

2
  • 1
    Another option to test this is: echo 0x7FFFFFFFFFFFFFFF; which should print 9223372036854775807 if integer is 64 bit.
    – Elmue
    Nov 17, 2017 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, 2017 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.

1
  • 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, 2017 at 19:34
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).

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 ..

2
  • 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. Nov 27, 2015 at 5: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, 2017 at 19:25
-1

This question is one of the very first problem being discovered in PHP lately.

I think is best to get the users' date from DateTimeImmutable and store it as a variable char in the database.

When retrieving the dates you can make a simple function to display dates in a format that you love.

E.g. date can be:

$date = "4069-03-27 00:00:00";
// store it as a variable char in the database

You can make a function to compare, subtract and also do a lot more with it.

1
  • As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.
    – Community Bot
    Dec 20, 2023 at 19:42

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.