php 为什么时间戳限制为 2038?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5879173/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 22:46:07  来源:igfitidea点击:

Why do timestamps have a limit to 2038?

phptimestampyear2038

提问by Shoe

I just found out, running a calendar script, that timestamps in PHP has a limit to 2038. What does it really mean? Why is it 2038 instead of 2050 or 2039? Why a limit if timestamps just count seconds from a given date (1970)?

我刚刚在运行日历脚本时发现,PHP 中的时间戳限制为 2038。这到底是什么意思?为什么是 2038 年而不是 2050 年或 2039 年?如果时间戳只是从给定日期(1970)计算秒数,为什么要限制?

采纳答案by Matthew Scharley

The limit is imposed by the 4 byte signed integers that most C libraries use for representing that count. Quick math (assumes 365 day years, not exactly correct):

该限制是由大多数 C 库用于表示该计数的 4 字节有符号整数强加的。快速数学(假设 365 天年,不完全正确):

2147483648 seconds ~ 68.1 years

This also implies a lower limit of ~1900. Some libraries have started to introduce 64 bit epoch counts, but they are few and far between for the moment.

这也意味着 ~1900 的下限。一些库已经开始引入 64 位纪元计数,但目前它们很少。

回答by Sander Marechal

The maximum value of a 32-bit integer is 2,147,483,647. If you add +1 to that, you get -2,147,483,647. 2,147,483,647 seconds from 01-01-1970 00:00:00 is January 19, 2038. If you add one more second, you get a date somewhere in 1902.

32 位整数的最大值为 2,147,483,647。如果你加上 +1,你会得到 -2,147,483,647。从 01-01-1970 00:00:00 开始的 2,147,483,647 秒是 2038 年 1 月 19 日。如果再增加一秒,您会得到 1902 年的某个日期。

回答by DhruvPathak

due to the limit of INT datatype on 32 bit machine

由于 32 位机器上 INT 数据类型的限制

http://php.net/manual/en/function.mktime.php

http://php.net/manual/en/function.mktime.php

From php.net : "The maximum possible date accepted by mktime() and gmmktime() is dependent on the current location time zone.

来自 php.net :“ mktime() 和 gmmktime() 接受的最大可能日期取决于当前位置时区。

For example, the 32-bit timestamp overflow occurs at 2038-01-19T03:14:08+0000Z. But if you're in a UTC -0500 time zone (such as EST in North America), the maximum accepted time before overflow (for older PHP versions on Windows) is 2038-01-18T22:14:07-0500Z, regardless of whether you're passing it to mktime() or gmmktime()."

例如,32 位时间戳溢出发生在 2038-01-19T03:14:08+0000Z。但是,如果您处于 UTC -0500 时区(例如北美的 EST),则溢出前的最大接受时间(对于 Windows 上的旧 PHP 版本)为 2038-01-18T22:14:07-0500Z,无论无论您是将它传递给 mktime() 还是 gmmktime()。”

回答by pocketfullofcheese

my guess is that it is stored in a fixed number of bits, which means a limit on how big the timestamp can get. We could do some math to figure it out exactly.

我的猜测是它存储在固定数量的位中,这意味着时间戳可以得到多大的限制。我们可以做一些数学计算来准确地计算出来。