在PHP中实现总小时,分钟和秒需要帮助

I am quite new to PHP I need help in implementing the total Hours and minutes in PHP.

I have an array of time in PHP in hours, minutes and seconds

(array) Array
(
    [0] => 01:10:00
    [1] => 01:20:30
    [2] => 00:00:10
)

My Expected output should be the total time in Hours, minutes and seconds (ex. 2:30:40)

$hours;
$minutes;
$seconds;

foreach( $thatArray as $v )
{
  $subArray = explode(':', $v);

  $hours   += $subArray[0];
  $minutes += $subArray[1];
  $seconds += $subArray[2];
}

// output $hours / $minutes / $seconds, correctly formatting, etc

Not going to give you the codez. here's how you can do it:

Get the individual components of the time using explode()

Start by adding the seconds. If the result is greater than 60, increment minutes and so on.

The easiest way would probably be to convert all of them to a seconds representation, sum them, and convert them back. For example:

function timeToSeconds($time) {
    $parts = explode(':', $time);

    return $parts[0] * 3600 + $parts[1] * 60 + $parts[2];
}

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

    return str_pad($hours, 2, '0', STR_PAD_LEFT) . ':' . str_pad($minutes, 2, '0', STR_PAD_LEFT) . ':' . str_pad($seconds, 2, '0', STR_PAD_LEFT);
}

$times = array('01:10:00', '01:20:30', '00:00:10');
$total = secondsToTime(array_sum(array_map('timeToSeconds', $times)));

Here's a demo.