Average Date Interval php

Hello, I need to calculate the average time between some DateInterval.

Actually, I have some Dateinterval:

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 result:

 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 these intervals. Here it is only minutes and seconds, but it can be a month or a year.

I cannot find a good way to easily figure it out. I can simply add each dateInterval with this conversion:

sec + 60xmin + 3600xHour ...

And they play with the module (%).

But I hope there is another way?

+6
source share
3 answers

You have to multiply minutes by 60, hours from 3600, etc., until only a few seconds remain. From here it is easy to calculate the average value.

+1
source

Well, until I found a better one, 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; } 

This is not perfect, but better than nothing.

+1
source

Please refer to this link http://php.net/manual/en/function.date-diff.php

A user / developer note has many date difference features.

Try them out. I hope one of them helps you.

-1
source

Source: https://habr.com/ru/post/921380/


All Articles