最大时间() | PHP

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3953333/
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 11:32:58  来源:igfitidea点击:

Maximum time() | PHP

phpdatetimedatetime

提问by MacMac

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.

这是一个愚蠢的问题,但是 a 的最大 INT 值time()和它的未来日期是多少,例如

1st January 2999

2999 年 1 月 1 日

Would time() ever get to that value? Going for a large time()value would return this:

time() 会达到那个值吗?追求一个大的time()价值会返回这个:

Thu 1st Jan 1970 1:00AM

1970 年 1 月 1 日星期四上午 1:00

A normal int date

正常的 int 日期

1287320788 - outputs today's date: Sun 17th Oct 2010 2:06PM

1287320788 - 输出今天的日期:2010 年 10 月 17 日星期日下午 2:06

But I'm only curious for the biggest int date and the last date.

但我只对最大的 int 日期和最后一个日期感到好奇。

回答by Pekka

The last 32-Bit Integer timestamp will be reached January 19, 2038. This is known as the Year 2038 problem.

最后一个 32 位整数时间戳将达到 2038 年 1 月 19 日。这被称为2038 年问题

回答by Gordon

PHP stores the highest integer number it can represent in the PHP_INT_MAXconstant:

PHP 将它可以表示的最大整数存储在PHP_INT_MAX常量中:

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.

如果您想处理超出此范围的日期,请考虑使用DateTime API,例如

$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

回答by oglgo

Remember, the Y2038 problem does not apply on 64-bit systems.

请记住,Y2038 问题不适用于 64 位系统。

回答by Vladyslav Savchenko

The shortest way I know is to get tomorrow's date:

我知道的最短方法是获取明天的日期:

date("Y-n-j", strtotime("+1 day"))

日期(“Ynj”,strtotime(“+1 天”))

date("Y-n-j", PHP_INT_MAX)on 64bit-systems gives potential dangerous value: 292277026596-12-4

date("Y-n-j", PHP_INT_MAX)在 64 位系统上给出了潜在的危险值: 292277026596-12-4

回答by sanmai

On 64-bit platforms PHP_INT_MAXdoes not reflect maximum INT value for 32-bit platforms. Here's how to get it:

在 64 位平台PHP_INT_MAX上不反映 32 位平台的最大 INT 值。获取方法如下:

$max32bitInt = PHP_INT_SIZE == 4 ? PHP_INT_MAX : PHP_INT_MAX>>32;

If you're always using 64-bit platform, just use:

如果您总是使用 64 位平台,只需使用:

PHP_INT_MAX>>32

回答by Alexander Behling

DateTime seems to use 32bit on 64bit servers, too. So you get into trouble.

DateTime 似乎也在 64 位服务器上使用 32 位。所以你会遇到麻烦。

I've solve it this way:

我是这样解决的:

new DateTime("99999/12/31 00:00:00");

Because, the date overflows the maximum length for DateTime, date use the maximum possibel value and returns a DateTime-object like this (inspected with var_dump) :

因为,日期溢出日期时间的最大长度,日期使用最大可能值并返回这样的日期时间对象(使用 var_dump 检查):

object(DateTime)#9 (3) { ["date"]=> string(19) "2031-09-12 00:00:00" ["timezone_type"]=> int(3) ["timezone"]=> string(13) "Europe/Berlin" } }

I'm not sure if it differs with the versions of PHP. I've tested it with version 5.4.

我不确定它是否与 PHP 的版本不同。我已经用 5.4 版对其进行了测试。