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 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动