[英]Get difference between 2 dates in hours minutes and seconds beyond 24 hours fails
I have the following dates and I would like the difference in the format hh:mm:ss
. 我有以下日期,我想要
hh:mm:ss
格式的区别。
I have the following code, 我有以下代码,
$from = "2019-05-20 23:59";
$to = "2019-05-27 00:00";
$expiry_time = new DateTime($to);
$current_date = new DateTime($from);
$diff = $expiry_time->diff($current_date);
return $diff->format('%H:%I:%S');
The above returns 00:01:00
, but I expect it to be 114:01:00
, since its 6 days ( 6*24 = 114
). 上面的返回
00:01:00
,但是我希望它是114:01:00
,因为它的6天( 6*24 = 114
)。
I have also tried 我也尝试过
$from = "2019-05-20 23:59";
$to = "2019-05-27 00:00";
$time_diff = strtotime($to) - strtotime($from);
return date('H:i:s', $time_diff);
Which also gives 00:01:00
这也给
00:01:00
What am i missing? 我想念什么? How can i convert this to show even beyond 24 hours?
如何将其转换为甚至显示超过24小时? I have checked get time difference in hours minutes and seconds to no avail.
我检查了以小时,分钟和几小时为单位的时差,但无济于事。
You can access the days
property of the $diff
object, and multiply it by 24 (as there are 24 hours in a day), then add the H
format (to get the remaining hours) before concatenating the minutes and seconds. 您可以访问
$diff
对象的days
属性,并将其乘以24(因为一天中有24小时),然后在连接分钟和秒之前添加H
格式(以获取剩余的小时数)。
The H
value will never exceed 24, as the hours in a day will never be above that. H
值永远不会超过24,因为一天中的小时数永远不会超过24。
$from = "2019-05-20 23:59";
$to = "2019-05-27 00:00";
$expiry_time = new DateTime($to);
$current_date = new DateTime($from);
$diff = $expiry_time->diff($current_date);
return ($diff->days*24 + $diff->format("%H")).$diff->format(':%I:%S'); // 144:01:00
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.