1

我正在使用 php 版本 5.3.8 而 mktime 不适合我

这是代码

$dated= mktime(0,0,0,11,5,2038);
var_dump($dated);

输出为 bool(false) 请建议我修复

4

3 回答 3

4

mktime 返回时间戳,它是一个整数(在 PHP 中它实际上是一个有符号整数)。因此 32 位系统的最大可能时间戳是19 Jan 2038 03:14:07 UTC(在发生整数溢出之后)。对于更大的时间戳,您需要 64 位整数。

于 2012-05-10T11:19:50.130 回答
3

来自PHP 手册

在 PHP 5.1.0 之前,任何已知版本的 Windows 和其他一些系统也不支持负时间戳。因此,有效年份的范围仅限于 1970 年到 2038 年。

一种可能的解决方案是使用ADOdb Date Time Library。该库通过用 PHP 浮点数(通常为 64 位)替换本机函数的有符号整数(通常为 32 位)克服了这些限制。

于 2012-05-10T11:13:01.117 回答
2

关于使用 C/C++ 库转换时间的三个重要事项。

标准库中的 gmtime() 或 localtime() 从 time_t 转换为 struct tm,但 time_t 的分辨率是从纪元开始的秒数。所以小数秒不会计算在内。

mktime() 从 struct tm 向后转换为 time_t,但如果输入日期超出范围,它将返回 -1。(参考 2038 年问题:http ://en.wikipedia.org/wiki/Year_2038_problem )

If you are not using 64bit timestamp, even you run programs on 64bit machines, you still have year 2038 problem. There are 64bit version functions like gmtime64(),localtime64(), mktime64() that may resolve the year out of range issue. (Refer page: http://pic.dhe.ibm.com/infocenter/iseries/v7r1m0/index.jsp?topic=%2Frtref%2Fmktime64.htm)

Answers that using boost library cannot solve the year 2038 problem. Notes that from its page: "Internally boost::gregorian::date is stored as a 32 bit integer type."

于 2013-06-27T20:38:38.527 回答