【问题标题】:Get difference between 2 dates in hours minutes and seconds beyond 24 hours fails在 24 小时后以小时分钟和秒为单位获取 2 个日期之间的差异失败
【发布时间】:2019-07-20 21:22:24
【问题描述】:

我有以下日期,我想要hh:mm:ss 格式的差异。

我有以下代码,

$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');

上面返回00:01:00,但我希望它是114:01:00,因为它已经6天了(6*24 = 114)。

我也试过

$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);

这也给了00:01:00

我错过了什么?我怎样才能将其转换为显示甚至超过 24 小时?我检查了get time difference in hours minutes and seconds 无济于事。

【问题讨论】:

  • 差异是 6 天 1 小时,使用您的代码,您可以获得小时部分,即 1 小时......它不会自动将天转换为小时

标签: php date datetime


【解决方案1】:

您可以访问$diff 对象的days 属性,并将其乘以24(因为一天有24 小时),然后在连接之前添加H 格式(以获取剩余的小时数)分和秒。

H 的值永远不会超过 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

【讨论】:

  • 我怎样才能为whatsapp创建一个api,比如android应用程序。你能为那个@Qirel 创建一个教程吗
  • 这是一项非常艰巨的任务,与这里的问题完全无关。 @派
猜你喜欢
  • 1970-01-01
  • 2017-07-16
  • 2013-05-14
  • 1970-01-01
  • 2012-05-24
  • 1970-01-01
  • 1970-01-01
  • 2021-05-14
  • 2013-11-04
相关资源
最近更新 更多