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

I have two times and I want to subtract them by using a PHP built-in function or Carbon. For example, I have following times:

  1. 00:05:10, i.e. 5 Minutes 10 Seconds
  2. 00:03:10, i.e. 3 Minutes 10 Seconds

If I subtract them, the total time would be 00:08:20. Can someone kindly guide me how I can make such a subtraction?

Convert to timestamp i think, after compare and substract

If you convert both times to unix timestamps, take away a "base timestamp" (For 00:00:00) from each, and then add them together you will get the number of seconds value for the 2 timestamps. Through some simple operations we can then get the total number of hours, minutes, and remaining seconds, then format them in your input style.

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