Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
bc77d7c0 | 4 | | Copyright CiviCRM LLC. All rights reserved. | |
6a488035 | 5 | | | |
bc77d7c0 TO |
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 | | |
6a488035 | 9 | +--------------------------------------------------------------------+ |
d25dd0ee | 10 | */ |
6a488035 TO |
11 | |
12 | /** | |
13 | * | |
14 | * @package CRM | |
ca5cec67 | 15 | * @copyright CiviCRM LLC https://civicrm.org/licensing |
6a488035 TO |
16 | */ |
17 | ||
18 | /** | |
19 | * Date time utilties | |
20 | */ | |
21 | class CRM_Utils_Time { | |
22 | ||
23 | /** | |
50bfb460 SB |
24 | * @var int |
25 | * the seconds offset from the real world time | |
6a488035 TO |
26 | */ |
27 | static private $_delta = 0; | |
28 | ||
29 | /** | |
fe482240 | 30 | * Get the time. |
6a488035 | 31 | * |
77855840 TO |
32 | * @param string $returnFormat |
33 | * Format in which date is to be retrieved. | |
6a488035 TO |
34 | * |
35 | * @return date | |
6a488035 | 36 | */ |
00be9182 | 37 | public static function getTime($returnFormat = 'YmdHis') { |
6a488035 TO |
38 | return date($returnFormat, self::getTimeRaw()); |
39 | } | |
40 | ||
41 | /** | |
fe482240 | 42 | * Get the time. |
6a488035 | 43 | * |
50bfb460 SB |
44 | * @return int |
45 | * seconds since epoch | |
6a488035 | 46 | */ |
00be9182 | 47 | public static function getTimeRaw() { |
6a488035 TO |
48 | return time() + self::$_delta; |
49 | } | |
50 | ||
51 | /** | |
fe482240 | 52 | * Set the given time. |
6a488035 | 53 | * |
77855840 TO |
54 | * @param string $newDateTime |
55 | * A date formatted with strtotime. | |
56 | * @param string $returnFormat | |
57 | * Format in which date is to be retrieved. | |
6a488035 TO |
58 | * |
59 | * @return date | |
6a488035 | 60 | */ |
00be9182 | 61 | public static function setTime($newDateTime, $returnFormat = 'YmdHis') { |
6a488035 TO |
62 | self::$_delta = strtotime($newDateTime) - time(); |
63 | return self::getTime($returnFormat); | |
64 | } | |
65 | ||
66 | /** | |
fe482240 | 67 | * Remove any time overrides. |
6a488035 | 68 | */ |
00be9182 | 69 | public static function resetTime() { |
6a488035 TO |
70 | self::$_delta = 0; |
71 | } | |
8010540a TO |
72 | |
73 | /** | |
74 | * Approximate time-comparison. $a and $b are considered equal if they | |
75 | * are within $threshold seconds of each other. | |
76 | * | |
77855840 TO |
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). | |
8010540a TO |
83 | * @return bool |
84 | */ | |
00be9182 | 85 | public static function isEqual($a, $b, $threshold = 0) { |
8010540a TO |
86 | $diff = strtotime($b) - strtotime($a); |
87 | return (abs($diff) <= $threshold); | |
88 | } | |
96025800 | 89 | |
6a488035 | 90 | } |