PHP 为什么 strtotime 使用 YYYY-MM-DD HH:MM:SS +15 小时给我错误的日期?

PHP Why does strtotime give me the wrong day using YYYY-MM-DD HH:MM:SS +15 hours?

当我调用 strtotime("2016-05-06 15:00:00 +15.98 hours") 时,我期望 2016-05-07 06:58:48 但我得到 2016-05-10 02:00:00。给出了什么?

你可以在这里自己测试:

  1. 使用 strtotime:http://php.fnlist.com/date_time/strtotime
  2. 将输出整数转换为时间戳:http://www.epochconverter.com/

试试这个:

//60 * 60 * 15.98 = 57,528 seconds
$add = round(60 * 60 * 15.98);

$timestamp = strtotime("2016-05-06 15:00:00") + $add;

$dt = date("Y-m-d H:i:s", $timestamp);

echo $dt; //2016-05-07 06:58:48

这将计算到 2016-05-07 06:58:48

至于为什么会误加15.98小时就比较复杂了。 PHP 报告了此问题的错误,但目前 PHP 中的日期格式不支持 浮点数。由于您不能直接在日期格式中使用浮点数,因此必须将“1.5 年”替换为“18 个月”,或者像这样进行运算并四舍五入:

//60 * 60 * 15.98 = 57,528 seconds
$timeToAdd = round(60 * 60 * 15.98);

然后像上面的例子一样调用strtotime()

$date = strtotime("2016-05-06 15:00:00") + $timeToAdd;

PHP

中的日期格式不支持浮点数

好像不能这样在strtotime中做加法。此外,已报告小数点错误 here

你可以做的是像 William 的回答一样在 2 个单独的变量中添加时间