PHP 时间减法不起作用
我试图显示 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)
谢谢
I'm trying to display the time x started, the time x finished, and how long x took to complete. I have the start and end displaying correctly, but the following subtraction gives me a bonkers answer.
// 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);
e.g. ( 01:24 - 01:23 = 07:01)
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
时间戳是自 1970 年以来的秒数,每个时间戳代表一个绝对时间点。因此,
$endTime - $startTime
会生成一些时间点,例如1975-04-12 07:01:52
。打印其中的小时和分钟部分当然会打印07:01
。时间戳本身虽然是以秒为单位的差异,所以你可以这样做:你当然应该查看
DateInterval
(看第三个示例)。Timestamps are seconds since 1970, each timestamp representing an absolute point in time. So
$endTime - $startTime
produces some point in time like1975-04-12 07:01:52
. Printing the hour and minute part of that will of course print07:01
. The timestamp itself though is the difference in seconds, so you can do:You should of course look into
DateInterval
(look at the 3rd example).