Question

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 ?

Was it helpful?

Solution

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.

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top