I have this foreach
code, and I want to sum the values that it returns, can someone help me with that please?
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>';
}
This code outputs:
00:17:20
00:00:06
00:00:02
And I want to sum those value and then output 00:17:28.
you can use something like:
<?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;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.