martin
martin

Reputation: 96891

strtotime returns different results on different PHP versions

I came across a weird problem with strtotime function returning different (and I think incorrect) results.

This is my test code and results:

$fromTime = strtotime('2013-10-22');
$toTime = strtotime('2013-10-28');
$days = ($toTime - $fromTime) / (3600 * 24);

var_dump($days, $fromTime, $toTime);

// PHP 5.2.5 on codepad.org - correct
// int(6)
// int(1382400000)
// int(1382918400)

// PHP 5.3.15, PHP 5.4.6 - incorrect I guess
// float(6.0416666666667)
// int(1382392800)
// int(1382914800)

You can see it live on http://codepad.org/gdTqFLqG.
Do you have any idea why is this happening?

Upvotes: 1

Views: 666

Answers (2)

Spudley
Spudley

Reputation: 168655

I agree with @N.B.'s suggestion to use the DateTime class instead -- You shouldn't really be using strtotime() for this kind of thing today.

However, as for why it's happening, look at the dates you're comparing.... what often happens between those dates? Yep, that's right -- daylight savings.

So my guess is that it's got nothing to do with the PHP version, and more to do with the timezone setup on the different platforms you're testing. One is set to use the UCT and the other is set to use a local timezone that has DST.

Upvotes: 3

knes
knes

Reputation: 883

You should use date_default_timezone_set(), cause in second test you got correct time with some UTC+- correction.

Upvotes: 0

Related Questions