我正在尝试显示 x 开始的时间、x 完成的时间以及 x 完成所需的时间。我的开始和结束显示正确,但下面的减法给了我一个疯狂的答案。
// to unix timestamps for subtraction
$startTime = strtotime($row['bp_rec_start']);
$endTime = strtotime($row['bp_rec_end']);
$timeTaken = $endTime - $startTime;
//back to date formats
$startTime = date('H:i',$startTime);
$endTime = date('H:i',$endTime);
$timeTaken = date('H:i',$timeTaken);
例如( 01:24 - 01:23 = 07:01)
谢谢
最佳答案
时间戳是自 1970 年以来的秒数,每个时间戳代表一个绝对时间点。因此,$endTime - $startTime
会生成一些时间点,例如 1975-04-12 07:01:52
。打印其中的小时和分钟部分当然会打印 07:01
。但时间戳本身是以秒为单位的差异,所以你可以这样做:
echo "Difference: $timeTaken seconds";
您当然应该查看DateInterval
(看第三个例子)。
关于PHP 时间减法不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6259742/