简体   繁体   中英

Get average time difference between a sequence of dates

It's an example how i calculate how many seconds between 2 date :

$diff = strtotime('2015-08-24 09:00:30') - strtotime('2015-08-24 09:00:15');
print_r($diff); // 15 seconds

Ok it's 2 date, my array look like :

$array = ['2015-08-24 09:00:30', '2015-08-24 09:00:15', '2015-08-24 09:00:00', '2015-08-24 09:00:45'];

Assumed this is comment date, how i tell you'll get comments every 15 seconds ?

Since you can already computer the difference between 2 dates, which you can put in a diff2dates method, you can do the following:

$totalDiffs = 0;

for($i = 1; $i < count($array); $i++)
{
    $diff = diff2dates($array[$i], $array[$i-1]);
    $totalDiffs += $diff;
}

$avgDiff = $totalDiffs / count($array);

Get the average of the timestamps from the below thread. https://stackoverflow.com/a/18512759/1235298

Let T_avg be the average timestamp, then

sum_of_diffs = mod(T_avg - T_i) i -> 1 to n avg_time_diff = sum_of_diffs/n

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM