php strtotime given different result on 64bit
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 differentexample
// 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.9production 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.13Now 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
-rizkyabdilahStrtotime 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:
if (!is_numeric($param) && ($timestamp = strtotime($param)) {
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.
精彩评论