Skip to content

Instantly share code, notes, and snippets.

@sarahhenkens
Created September 13, 2012 21:53
Show Gist options
  • Save sarahhenkens/3717980 to your computer and use it in GitHub Desktop.
Save sarahhenkens/3717980 to your computer and use it in GitHub Desktop.
Assert to test that 2 time's are equal to each other within a margin of x seconds
/**
* Assert that 2 datetime strings are equal within a margin
*
* @param string $expected
* @param string $result
* @param integer $margin The margin in seconds
* @param string $message optional message
* @return boolean
*/
public function assertDateEquals($expected, $result, $margin = 1, $message = '') {
$expectedTime = strtotime($expected);
$resultTime = strtotime($result);
$upper = $expectedTime + $margin;
$lower = $expectedTime - $margin;
if (!$message) {
$message = sprintf('Failed asserting that %s is equal to %s within a margin of %d seconds', $result, $expected, $margin);
}
return self::assertTrue((($resultTime <= $upper) && ($resultTime >= $lower)), $message);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment