Skip to content

Instantly share code, notes, and snippets.

@connordavison
Created October 14, 2015 08:56
Show Gist options
  • Save connordavison/46896e94f145b5b83ee8 to your computer and use it in GitHub Desktop.
Save connordavison/46896e94f145b5b83ee8 to your computer and use it in GitHub Desktop.
Calculating standard deviation/average of differences between a list of DateTimes, and determining if a given diff is unlikely.
<?php
/**
* Yields the list of differences between a list of datetimes.
* @param array $dates An ordered list of datetimes of which to find the
* differences.
* @return array The differences between each sequential datetime in $dates.
*/
function date_diffs($dates) {
$diffs = array();
for ($i = 1; $i < count($dates); $i++) {
$diffs[] = $dates[$i]->getTimestamp() - $dates[$i - 1]->getTimestamp();
}
return $diffs;
}
$diffs = date_diffs($dates);
$avg = array_sum($diffs) / (count($dates) + 1);
$stddev = stats_standard_deviation($diffs);
if ($some_time_diff > $avg + 3 * $stddev) {
// There must be something wrong!
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment