drupal-civicrm/sites/all/modules/civicrm/CRM/Utils/Time.php
2018-01-14 13:10:16 +00:00

107 lines
3.3 KiB
PHP

<?php
/*
+--------------------------------------------------------------------+
| CiviCRM version 4.7 |
+--------------------------------------------------------------------+
| Copyright CiviCRM LLC (c) 2004-2017 |
+--------------------------------------------------------------------+
| This file is a part of CiviCRM. |
| |
| CiviCRM is free software; you can copy, modify, and distribute it |
| under the terms of the GNU Affero General Public License |
| Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
| |
| CiviCRM is distributed in the hope that it will be useful, but |
| WITHOUT ANY WARRANTY; without even the implied warranty of |
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| See the GNU Affero General Public License for more details. |
| |
| You should have received a copy of the GNU Affero General Public |
| License and the CiviCRM Licensing Exception along |
| with this program; if not, contact CiviCRM LLC |
| at info[AT]civicrm[DOT]org. If you have questions about the |
| GNU Affero General Public License or the licensing of CiviCRM, |
| see the CiviCRM license FAQ at http://civicrm.org/licensing |
+--------------------------------------------------------------------+
*/
/**
*
* @package CRM
* @copyright CiviCRM LLC (c) 2004-2017
*/
/**
* Date time utilties
*/
class CRM_Utils_Time {
/**
* @var int
* the seconds offset from the real world time
*/
static private $_delta = 0;
/**
* Get the time.
*
* @param string $returnFormat
* Format in which date is to be retrieved.
*
* @return date
*/
public static function getTime($returnFormat = 'YmdHis') {
return date($returnFormat, self::getTimeRaw());
}
/**
* Get the time.
*
* @return int
* seconds since epoch
*/
public static function getTimeRaw() {
return time() + self::$_delta;
}
/**
* Set the given time.
*
* @param string $newDateTime
* A date formatted with strtotime.
* @param string $returnFormat
* Format in which date is to be retrieved.
*
* @return date
*/
public static function setTime($newDateTime, $returnFormat = 'YmdHis') {
self::$_delta = strtotime($newDateTime) - time();
return self::getTime($returnFormat);
}
/**
* Remove any time overrides.
*/
public 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
*/
public static function isEqual($a, $b, $threshold = 0) {
$diff = strtotime($b) - strtotime($a);
return (abs($diff) <= $threshold);
}
}