$phpNow = date('Y-m-d H:i');
$sqlNow = CRM_Core_DAO::singleValueQuery("SELECT date_format(now(), '%Y-%m-%d %H:%i')");
- if ($phpNow != $sqlNow) {
+ if (!CRM_Utils_Time::isEqual($phpNow, $sqlNow, 2.5 * 60)) {
$messages[] = new CRM_Utils_Check_Message(
'checkMysqlTime',
ts('Timestamps reported by MySQL (eg "%2") and PHP (eg "%3" ) are mismatched.<br /><a href="%1">Read more about this warning</a>', array(
- 1 => CRM_Utils_System::getWikiBaseURL() . 'checkMysqlTime',
+ 1 => CRM_Utils_System::getWikiBaseURL() . 'checkMysqlTime',
2 => $sqlNow,
3 => $phpNow,
)),
static function resetTime() {
self::$_delta = 0;
}
+
+ /**
+ * Approximate time-comparison. $a and $b are considered equal if they
+ * are within $threshold seconds of each other.
+ *
+ * @param string $a time which can be parsed by strtotime
+ * @param string $b time which can be parsed by strtotime
+ * @param int $threshold maximum allowed difference (in seconds)
+ * @return bool
+ */
+ static function isEqual($a, $b, $threshold = 0) {
+ $diff = strtotime($b) - strtotime($a);
+ return (abs($diff) <= $threshold);
+ }
}
--- /dev/null
+<?php
+
+require_once 'CiviTest/CiviUnitTestCase.php';
+
+class CRM_Utils_TimeTest extends CiviUnitTestCase {
+ public function equalCases() {
+ $cases = array(); // array(0 => $timeA, 1 => $timeB, 2 => $threshold, 3 => $expectedResult)
+ $cases[] = array('2012-04-01 12:00:00', '2012-04-01 12:00:00', 0, 1);
+ $cases[] = array('2012-04-01 12:00:00', '2012-04-01 12:00:01', 0, 0);
+ $cases[] = array('2012-04-01 12:00:00', '2012-04-01 12:00:50', 60, 1);
+ $cases[] = array('2012-04-01 12:00:00', '2012-04-01 12:01:02', 60, 0);
+ $cases[] = array('2012-04-01 12:00', '2012-04-01 12:01', 0, 0);
+ $cases[] = array('2012-04-01 12:00', '2012-04-01 12:01', 60, 1);
+ $cases[] = array('2012-04-01 12:00', '2012-04-01 12:01', 120, 1);
+ return $cases;
+ }
+
+ /**
+ * @param string $timeA
+ * @param string $timeB
+ * @param int $threshold
+ * @param bool $expectedResult
+ * @dataProvider equalCases
+ */
+ function testEquals($timeA, $timeB, $threshold, $expectedResult) {
+ $actual = CRM_Utils_Time::isEqual($timeA, $timeB, $threshold);
+ $this->assertEquals($expectedResult, $actual);
+
+ $actual = CRM_Utils_Time::isEqual($timeB, $timeA, $threshold);
+ $this->assertEquals($expectedResult, $actual);
+ }
+}
\ No newline at end of file