Skip to content

Instantly share code, notes, and snippets.

@faulker
Last active December 18, 2015 05:29
Show Gist options
  • Save faulker/5733140 to your computer and use it in GitHub Desktop.
Save faulker/5733140 to your computer and use it in GitHub Desktop.
Analyzes an array of date/time stamps to try and make a "best" guess on when the next date/time stamp will be.
function get_average($time_array)
{
$total = "";
$last = "";
$count = count($time_array);
for($t=0;$t<$count;$t++)
{
$time = $time_array[$t];
if($t == 0)
{
$last = $time;
} else {
$diff = get_diff($last, $time);
$last = $time;
$total += $diff;
}
}
$average = ($total/($count-1));
return round($average);
}
function get_diff($time1, $time2)
{
$t1 = strtotime($time1);
$t2 = strtotime($time2);
$diff = ($t2 - $t1)/60;
return $diff;
}
function guess_next($date_array)
{
$diff_avg = get_average($date_array);
$last_date = new DateTime(end($date_array));
$last_date->add(new DateInterval('PT'.$diff_avg.'M'));
$next = $last_date->format('m/d/Y H:i');
return $next;
}
$date_stamps = array(
"5/10/2013 9:30",
"5/11/2013 9:50",
"5/12/2013 10:20",
"5/13/2013 10:59",
"5/14/2013 11:22",
"5/15/2013 11:51",
"5/16/2013 12:28",
"5/17/2013 12:57",
"5/18/2013 13:13");
print(guess_next($date_stamps));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment