php strtotime 在 64 位上给出了不同的结果
我有一个名为 _to_unix_timestamp() 的 php 函数 代码:
function _to_unix_timestamp($param){
if ($timestamp = strtotime($param)){
return $timestamp;
}
return (int) $param;
}
代码在我的开发服务器(32 位)上运行良好
但是当我在生产服务器(64 位)中部署应用程序时
略有不同
该函数的输出与我的开发服务器上的示例
// expected to be "int(1306400175)" but the output is "int(-56632154432)"
var_dump(_to_unix_timestamp("1306400175"));
,输出为 int(1306400175)
但在生产服务器上,输出是 int(-56632154432)
只是信息 开发服务器(32位)=
Linux glustervm 2.6.18-164.el5xen #1 SMP 9 月 3 日星期四 04:47:32 EDT 2009 i686 i686 i386 GNU/Linux
PHP 5.2.9
生产服务器(64 位)=
Linux minicapella 2.6.18-164.15.1.el5.centos.plusxen #1 SMP 3 月 17 日星期三 20:32:20 EDT 2010 x86_64 x86_64 x86_64 GNU/Linux
PHP 5.2.13
现在,我追加后该函数运行良好
//append "> 0"
if ($timestamp = strtotime($param) > 0){
但是,我想知道为什么输出不同?
谢谢你的建议
-里兹基阿卜迪拉
I have php function called _to_unix_timestamp()
the code :
function _to_unix_timestamp($param){
if ($timestamp = strtotime($param)){
return $timestamp;
}
return (int) $param;
}
The code is running well on my development server (32bit)
but when I deploy my application in production server (64bit)
the output from this function is little bit different
example
// expected to be "int(1306400175)" but the output is "int(-56632154432)"
var_dump(_to_unix_timestamp("1306400175"));
on my development server, the output is int(1306400175)
but on production server, the output is int(-56632154432)
just info
development server (32bit) =
Linux glustervm 2.6.18-164.el5xen #1 SMP Thu Sep 3 04:47:32 EDT 2009 i686 i686 i386 GNU/Linux
PHP 5.2.9
production server (64bit) =
Linux minicapella 2.6.18-164.15.1.el5.centos.plusxen #1 SMP Wed Mar 17 20:32:20 EDT 2010 x86_64 x86_64 x86_64 GNU/Linux
PHP 5.2.13
Now the function is running well after I append
//append "> 0"
if ($timestamp = strtotime($param) > 0){
But, I want to know why the output is different?
Thanks for your advice
-rizkyabdilah
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Strtotime 的工作原理是读取您以字符串形式提供的内容,并尝试在其中查找日期。因此,当您传递“1306400175”时,它会尝试查找 YEAR-MONTH-DAY-HOUR-... 格式,它知道与其匹配,并且由于它可能确实找到了某些东西,因此它返回一个值你对应于它认为他找到的那一天。
换句话说,它并不像您想象的那样工作,例如,如果您执行 strtotime("1400") ,它将返回与今天 14:00 对应的时间戳。所以你的代码实际上在 32 位和 64 位中都失败了,它是不同结果的原因可能是因为当它的值超过 PHP_INT_MAX 时整数溢出(它达到了它可以拥有的最高可能值,所以它回到了它的最低值)可以有),这在 32 位和 64 位 PHP 之间是不同的。
对您的函数的实际修复是在将 param 传递给 strtotime 之前检查它是否不是数字:
尽管您需要确保您打算做的确实是解析日期/时间字符串(例如“2011-04 -08 14:00") 转换为相应的时间戳。
Strtotime works by reading what you give it as a string, and try to find a date somewhere in it. So when you passed it "1306400175" it tried to find a YEAR-MONTH-DAY-HOUR-... format it knows that matches it, and since it probably does find something it returns a value to you corresponding to the day it think he found.
In other words, it just doesn't work the way you think, for exemple if you do strtotime("1400") it will return a timestamp corresponding to TODAY at 14:00. So your code actually fails both in 32 and 64 bits, the reason it is a different result is probably because the integer overflows when its value exceed PHP_INT_MAX (it reaches the highest possible values it can have, so it goes back to the lowest value it can have), which is different between 32 and 64 bits PHP.
An actual fix for your function would be to check that param is not a number before passing it to strtotime:
Although you need to make sure that what you intend to do is indeed to parse a date/time string (such as "2011-04-08 14:00") into a corresponding timestamp.