How can I save in dteDiff the sum of the differences between dates that I do in the loop?
$dteDiff = ??????; // <-- no sé cómo definirlo aquí
for ($i=1; $i < sizeof($Result); $i+=2) {
$strStart = $Result[$i-1]['tiempo'];
$strEnd = $Result[$i]['tiempo'];
$dteStart = new DateTime($strStart);
$dteEnd = new DateTime($strEnd);
$dteDiff += $dteStart->diff($dteEnd);
}
$tiempoTemporal = $dteDiff->format("%H:%I:%S");
echo $tiempoTemporal;
diff returns an object of type DateInterval . You have to handle it as such.
I have declared DateDiff as the current date. In each iteration I am adding the difference between the dates.
The end result is the current date forwarded by the sum of all the diffs. So if we calculate the difference between this and the current date that we stored before, we get the total time difference.
Other than this, the only change I've added is changing the operator
+=
(which doesn't work for dates) to theadd
.I have solved it in this way with the help of this guide that the user @fedorqui has left me in the comments
What it does is define an object
DateTime
and add the onesdiff
I get from each interval to it.