I have to make a calculation with times like these that come from an array:
+04:43
03:33
-10:33
I tried using Datetime and stuff but the class just broke when it surpassed 24:00 time (10:00 + 20:00 for example). So I tried something like this:
I transformed all my hh:mm to seconds with explode:
foreach($stringSaldo as $saldo) {
$horaM[] = explode(':',$saldo);
}
$totalHora = 0;
$totalMin = 0;
foreach($horaM as $hora) {
$totalHora = ($totalHora + $hora[0]);
$totalMin =( $totalMin + $hora[1]);
}
$totalHora = $totalHora * 3600;
$totalMin = $totalMin * 60;
$totalSeconds = $totalHora + $totalMin;
Then I tried to make that seconds in time:
$hours = floor($totalSeconds / 3600);
$minutes = floor(($totalSeconds / 60) % 60);
$seconds = $totalSeconds % 60;
echo $hours. ":" . $minutes;
For some reason when I have times like: -03:34
and +01:00
the calculation fails, it gives -02:-26
but it should be -02:34
.
What am I doing wrong?