我是PHP新手我需要帮助实现PHP的总小时和分钟。
我在PHP中有几小时,几分钟和几秒的时间
(array) Array
(
[0] => 01:10:00
[1] => 01:20:30
[2] => 00:00:10
)
我的预期输出应为以小时,分钟和秒为单位的总时间(例如2:30:40)
答案 0 :(得分:0)
$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
答案 1 :(得分:0)
答案 2 :(得分:0)
最简单的方法可能是将所有这些转换为秒表示,对它们求和,然后将它们转换回来。例如:
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)));