S
S
Sama Samsonov2018-04-12 15:20:36
PHP
Sama Samsonov, 2018-04-12 15:20:36

How to calculate the sum of hours?

There is an array of hours $arraytime(
07:01:05, 06:21:30, 08:15:15, 07:13:08, 06:02:00, 09:21:10, 12:54:48
)
How to calculate in hour format 57:17:56
I wrote

$result = 0;
    foreach ($arraytimeas $elem) {
      $result = strtotime($result) + strtotime($elem);
    }
echo $result; // выдает 55 т.е. минуты и секунды не сложить и не показывает как исправить

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
alexalexes, 2018-04-12
@samuser

A little more difficult than your previous task. Try to figure out how to work with time on your own.

$arraytime = ['07:01:05', '06:21:30', '08:15:15', '07:13:08', '06:02:00', '09:21:10', '12:54:48'];
$sum = 0;
foreach($arraytime as $time)
    $sum += strtotime($time) - strtotime('00:00:00');
echo sprintf('%d:', $sum / 3600).date('i:s', $sum);

A
Anton fon Faust, 2018-04-12
@bubandos

$sumH = 0;
$sumM = 0;
$sumS = 0;
foreach($arraytime as $time) {
    list($H, $M, $S) = explode(':',$time);
    $sumH += $H;
    $sumM += $M;
    $sumS += $S;
}
$newS = $sumS % 60;
$sumM += intval($sumS / 60);
$newM = $sumM % 60;
$sumH += intval($sumM / 60);
$result = implode(':', [$sumH, $newM, $newS]);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question