Accessing dates in PHP beyond 2038

2019-01-03 03:29发布

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?

5条回答
看我几分像从前
2楼-- · 2019-01-03 03:42

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

查看更多
你好瞎i
3楼-- · 2019-01-03 03:44

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

查看更多
孤傲高冷的网名
4楼-- · 2019-01-03 03:51

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.

查看更多
Emotional °昔
5楼-- · 2019-01-03 03:59

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

查看更多
Deceive 欺骗
6楼-- · 2019-01-03 04:00

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

查看更多
登录 后发表回答