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

报告相同问题?

悬赏问题

  • ¥15 C语言设计一个简单的自动换档程序
  • ¥15 关于logstash转发日志时发生的部分内容丢失问题
  • ¥17 pro*C预编译“闪回查询”报错SCN不能识别
  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。