Created
October 14, 2015 08:56
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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