| 1 | <?php |
| 2 | /* |
| 3 | +--------------------------------------------------------------------+ |
| 4 | | Copyright CiviCRM LLC. All rights reserved. | |
| 5 | | | |
| 6 | | This work is published under the GNU AGPLv3 license with some | |
| 7 | | permitted exceptions and without any warranty. For full license | |
| 8 | | and copyright information, see https://civicrm.org/licensing | |
| 9 | +--------------------------------------------------------------------+ |
| 10 | */ |
| 11 | |
| 12 | /** |
| 13 | * |
| 14 | * @package CRM |
| 15 | * @copyright CiviCRM LLC https://civicrm.org/licensing |
| 16 | */ |
| 17 | |
| 18 | /** |
| 19 | * Date time utilties |
| 20 | */ |
| 21 | class CRM_Utils_Time { |
| 22 | |
| 23 | /** |
| 24 | * @var int |
| 25 | * the seconds offset from the real world time |
| 26 | */ |
| 27 | static private $_delta = 0; |
| 28 | |
| 29 | /** |
| 30 | * Get the time. |
| 31 | * |
| 32 | * @param string $returnFormat |
| 33 | * Format in which date is to be retrieved. |
| 34 | * |
| 35 | * @return date |
| 36 | */ |
| 37 | public static function getTime($returnFormat = 'YmdHis') { |
| 38 | return date($returnFormat, self::getTimeRaw()); |
| 39 | } |
| 40 | |
| 41 | /** |
| 42 | * Get the time. |
| 43 | * |
| 44 | * @return int |
| 45 | * seconds since epoch |
| 46 | */ |
| 47 | public static function getTimeRaw() { |
| 48 | return time() + self::$_delta; |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * Set the given time. |
| 53 | * |
| 54 | * @param string $newDateTime |
| 55 | * A date formatted with strtotime. |
| 56 | * @param string $returnFormat |
| 57 | * Format in which date is to be retrieved. |
| 58 | * |
| 59 | * @return date |
| 60 | */ |
| 61 | public static function setTime($newDateTime, $returnFormat = 'YmdHis') { |
| 62 | self::$_delta = strtotime($newDateTime) - time(); |
| 63 | return self::getTime($returnFormat); |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Remove any time overrides. |
| 68 | */ |
| 69 | public static function resetTime() { |
| 70 | self::$_delta = 0; |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Approximate time-comparison. $a and $b are considered equal if they |
| 75 | * are within $threshold seconds of each other. |
| 76 | * |
| 77 | * @param string $a |
| 78 | * Time which can be parsed by strtotime. |
| 79 | * @param string $b |
| 80 | * Time which can be parsed by strtotime. |
| 81 | * @param int $threshold |
| 82 | * Maximum allowed difference (in seconds). |
| 83 | * @return bool |
| 84 | */ |
| 85 | public static function isEqual($a, $b, $threshold = 0) { |
| 86 | $diff = strtotime($b) - strtotime($a); |
| 87 | return (abs($diff) <= $threshold); |
| 88 | } |
| 89 | |
| 90 | } |