I was working on a client’s web site using the environment on my local machine and everything seemed to be working just fine. However, on the client’s live web site, which is hosted by 1and1, something just wasn’t working. I narrowed the problem down to different values being return by the PHP strtotime function in php.
Here was the code running on my MAC:
<!--?php echo strtotime("2100-01-01") . " " . strtotime("2100-01-02") ?-->
This echoes 4102473600 4102560000 to my screen. If you subtract the first number from the second, (4102560000 – 4102473600), you get 86400. 86400 = 60 * 60 * 24, which is the number of seconds in a day. So far so good.
However, when I uploaded the SAME EXACT code to my client’s web site and tested it out, the output was empty. I looked at the documentation for strtotime (http://php.net/manual/en/function.strtotime.php) and discovered the function can return false. so I tried running this code on my local server:
<!--?php
if (! strtotime("2100-01-01"))
{
echo "strtotime failed";
}
?-->
Nothing printed out locally. When uploaded to my 1and1 environment, “strtotime failed” was printed to the screen. After some digging, I noticed that the machine that was hosting my 1and1 account was using a 32-bit processor. On a 32 bit processor, the largest positive integer that can be represented is 2147483647. 4102560000 and 4102473600 are both greater than 2147483647, and can’t be represented by simple means on a 32 bit machine. The function was failing because of overflow. It turns out that maximum future date and time that can be represented by normal means on a 32 bit machine is Tue, 19 Jan 2038 03:14:07 GMT, which corresponds to 2147483647.
Luckily I only chose 2100-01-02 as an upper bound. For my needs, I required a date that was far off in the future. Unfortunately this is too far off for a 32 bit machine to easily represent. I simply chose 2038-01-01, which is a date far into the future that can be represented on both a 32 and 64 bit machine.
Lesson learned, if strotime acts differently in different environments, start off by finding out what kind of processors are running in each environment. If they are different, you have a good starting point in diagnosing whatever problem you have. For more information about representing larger numbers on 32 bit machines, check out this link: http://www.php.net/manual/en/book.gmp.php