繁体   English   中英

PHP的strtotime +2.5小时的错误?

[英]php strtotime +2.5 hours bug?

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

预期的行为是$ plusHrs = $ plusSec。 无论输入什么日期,都会发生这种情况,这意味着它不是夏令时。

这是错误吗? (PHP v5.3.14)

不,这不是错误。 strtotime()需要相对格式的整数值。 因此,您的+2.5小时将被视为"+2 GMT" ,然后 "+5"小时和点将被忽略。 您可以将其更改为逗号,甚至也可以删除-结果不会更改,因为同样,仅会解析整数。 因此,此类声明将被视为相对于GMT偏移的相对小时数:

//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'));

因此,例如, +1.5 hoursadd 5 hours for time in GMT +1 这意味着,最终结果将取决于当前时区,因为初始时间戳将设置为当前时区( now在上面的示例中)。

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

These two are different, $s = $d * 60 * 60 while $d = '2.5.

您可以尝试使用2.5小时

$plusHrs = strtotime($t.' + 2 hours 30 minutes'); 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM