我有这个foreach
代码,我想总结它返回的值,有人可以帮我吗?
foreach($timel->results() as $timel) {
$timeDiff = strtotime($timel->end_date) - strtotime($timel->start_date);
$hours = floor($timeDiff / 3600);
$remainder = $timeDiff - $hours * 3600;
$formattedTime = sprintf('%02d', $hours) . gmdate(':i:s', $remainder);
echo $formattedTime, '<br>';
}
此代码输出:
0时17分20秒
0点00分06秒
00:00:02
我希望将这些值相加,然后输出00:17:28。
答案 0 :(得分:0)
你可以使用类似的东西:
<?php
$totaltime = 0;
foreach($timel->results() as $timel) {
$timeDiff = strtotime($timel->end_date) - strtotime($timel->start_date);
$totaltime += $timeDiff;
}
$hours = floor($totaltime / 3600);
$remainder = $totaltime - $hours * 3600;
$formattedTime = sprintf('%02d', $hours) . gmdate(':i:s', $remainder);
echo $formattedTime;