如何使用碳在PHP中减去两次?

时间:2019-02-26 12:17:14

标签: php time

我有两次,我想使用PHP内置函数或Carbon减去它们。例如,我有以下时间:

  1. <script src="https://unpkg.com/popmotion/dist/popmotion.global.min.js"></script> <img class="dial" src="https://greensock.com/wp-content/uploads/custom/draggable/img/knob.png">,即5分钟10秒
  2. 00:05:10,即3分钟10秒

如果我减去它们,总时间将为00:08:20。有人可以指导我如何进行减法吗?

2 个答案:

答案 0 :(得分:0)

比较并减去后,转换为我认为的时间戳

答案 1 :(得分:0)

如果将两个时间都转换为unix时间戳,请从每个时间戳中减去一个“基本时间戳”(对于00:00:00),然后将它们加在一起,您将获得2个时间戳的秒数值。通过一些简单的操作,我们可以得到小时,分钟和剩余秒的总数,然后以您的输入样式进行格式化。

function add_times($a, $b) {
    $base = strtotime('00:00:00');
    $seconds = (strtotime($a) - $base) + (strtotime($b) - $base);

    $hours = floor($seconds / 3600);
    $minutes = floor($seconds / 60) % 60;
    $seconds = $seconds % 60;

    return sprintf('%02d:%02d:%02d', $hours, $minutes, $seconds);
}

echo add_times('00:05:10', '00:03:10');