php - calculate average interval on array of dates -
i have array dates
array ( [0] => 2014-09-05 [1] => 2014-09-09 [2] => 2014-09-09 [3] => 2014-09-11 [4] => 2014-09-16 [5] => 2014-09-18 [6] => 2014-09-25 )
look interval between dates approximately 3 days.
how can calculate interval automatically?
this should work you:
just loop through of dates , check if there still next date. if yes subtract both timestamps , add $intervals
array.
at end take average of intervals, in seconds, can divide 3600 * 24
average day interval.
<?php $dates = [ "2014-09-05", "2014-09-09", "2014-09-09", "2014-09-11", "2014-09-16", "2014-09-18", "2014-09-25", ]; foreach($dates $key => $date) { if(isset($dates[($key+1)])) $intervals[] = abs(strtotime($date) - strtotime($dates[($key+1)])); } $average = array_sum($intervals) / count($intervals); echo $average / (3600 * 24); ?>
output:
_ 3.33 //if want can round
Comments
Post a Comment