Question

Having some unexpected behaviour with strtotime:

$d = '2.5';                                   // i.e. hours
$t = '2014-03-20 08:30:00';                   // mysql datetime
$ut = strtotime($t);                          // value: 1395297000
$s = $d * 60 * 60;                            // duration in seconds: 9000
$plusHrs = strtotime($t.' +'.$d.' hours');    // value: 1395315000
$plusSec = strtotime($t.' +'.$s.' seconds');  // value: 1395306000

Expected behaviour would be that $plusHrs = $plusSec. This happens regardless of what date is input, which means it's not a daylight saving issue.

Is this a bug? (PHP v5.3.14)

Was it helpful?

Solution

No, it isn't a bug. strtotime() expects integer values in relative format. Thus, your +2.5 hours would be treated as "+2 GMT" and then "+5" hours and dot would be just ignored. You can change it to comma or even delete as well - result won't change since, again, only integers are parsed. So, such statement will be treated as relative hours addition with respect of GMT offset:

//this is the same things:
//and they mean set timesone to GMT+2 & add 5 hours:
date('Y-m-d H:i:s', strtotime('now +2.5 hours'));
date('Y-m-d H:i:s', strtotime('now +2,5 hours'));
date('Y-m-d H:i:s', strtotime('now +2 5 hours'));

So, for example, +1.5 hours will do add 5 hours for time in GMT +1. That means, end result will depend of current timezone since initial timestamp will be set to current timezone (it's now in sample above).

OTHER TIPS

you can try with this for hours 2.5

$plusHrs = strtotime($t.' + 2 hours 30 minutes'); 
$plusHrs = strtotime($t.' +'.$d.' hours');    
$plusSec = strtotime($t.' +'.$s.' seconds'); 

These two are different, $s = $d * 60 * 60 while $d = '2.5.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top