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. 将输出int转换为时间戳:http://www.epochconverter.com/

PHP中的日期格式不支持浮动

似乎你不能用这种方式在时间上做加法。此外,在这里报告了一个小数点错误

您可以将时间添加到两个单独的变量中,就像William的答案

一样

试试这个:

//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中的日期格式中不支持浮动。由于您不能直接在日期格式中使用浮动,您必须用"18个月"代替"1.5年",或者在它之前进行算术运算,然后像这样四舍五入:

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

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

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