dongyirong3564 2014-04-02 07:21
浏览 403
已采纳

php strtotime +2.5小时的bug?

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)

  • 写回答

3条回答 默认 最新

  • dongping8572 2014-04-02 07:28
    关注

    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).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥170 如图所示配置eNSP
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥15 键盘指令混乱情况下的启动盘系统重装