I have an array of DateTime() objects in which I store some time duration (like "H:i") How can I sum all elements in this array to get total time duration?
And it's also should be taken into account, that if total time duration is grater that 23:59, I have to be able to get nubmer of days.
is this possible?
I was trying to do like this:
$duration = new DateTime('00:00');
foreach($routes as $route) {
$arrival_time = new DateTime();
$arrival_time->setTimestamp($route->arrival_time);
$departure_time = new DateTime();
$departure_time->setTimestamp($route->departure_time);
$leg_duration = $arrival_time->diff($departure_time);
$duration->add($leg_duration);
}
but in $duration
I got wrong time.
P.S.
Using $duration->add($leg_duration);
I got subtracted from "24:00"
time, why?
For example if $leg_duration = new DateTime('02:10');
the result will be "21:50"
.
$duration->sub($leg_duration);
add time to "24:00"
Is this right?