Average dateInterval php

1k Views Asked by At

Hello i need to calculate an average time between some DateInterval.

Actually i've some Dateinterval like this :

for ($i = 0 ; $i < count($startDate) ; $i++)
    {
          $diffTable[] = date_diff($finishDate[$i], $startDate[$i]);
          echo $diffTable[$i]->format("%Y-%M-%d %H:%i:%s");
    }

Here is the output :

00-00-0 00:13:17
00-00-0 00:7:47
00-00-0 00:7:14
00-00-0 00:10:39

I need to calculate the average time between this intervals. Here it's only minute and second, but it could be Month or year.

I can't find a good way to calculate it easily. i can simply add every dateInterval with a conversion like this :

sec + 60xmin + 3600xHour ...

And them play with Modulo (%).

But i hope there is another way ?

2

There are 2 best solutions below

0
On BEST ANSWER

You should multiply the minutes with 60, the hours with 3600, etc., until there's only seconds left. From there it's easy to calculate the average.

0
On

Ok untif i found sth better i just write this :

function dateIntervalToSecond($interval)
    {
        return $interval->y     * 31556926 
                + $interval->m  * 2629743
                + $interval->d  * 6400
                + $interval->h  * 3600
                + $interval->i  * 60
                + $interval->s;
    }

It's not perfect, But better than nothing.