X-Git-Url: https://vcs.fsf.org/?a=blobdiff_plain;f=CRM%2FUtils%2FTime.php;h=dd56d6b9e422125c1c3832cbbf6b521e6b3c87b5;hb=9658bf9b5fac458661af179b73b49c5372d05dfe;hp=b700e572071092ee7c9f1462b914613b876c406f;hpb=9c204b425c4f161f3dec52e3d7f0cae2f9f35eb5;p=civicrm-core.git diff --git a/CRM/Utils/Time.php b/CRM/Utils/Time.php index b700e57207..dd56d6b9e4 100644 --- a/CRM/Utils/Time.php +++ b/CRM/Utils/Time.php @@ -30,6 +30,50 @@ class CRM_Utils_Time { */ static private $callback = NULL; + /** + * Evaluate a time expression (relative to current time). + * + * @param string $str + * Ex: '2001-02-03 04:05:06' or '+2 days' + * @param string|int $now + * For relative time strings, $now determines the base time. + * @return false|int + * The indicated time (seconds since epoch) + * @see strtotime() + */ + public static function strtotime($str, $now = 'time()') { + if ($now === NULL || $now === 'time()') { + $now = self::time(); + } + return strtotime($str, $now); + } + + /** + * Format a date/time expression. + * + * @param string $format + * Ex: 'Y-m-d H:i:s' + * @param null|int $timestamp + * The time (seconds since epoch). NULL will use current time. + * @return string + * Ex: '2001-02-03 04:05:06' + * @see date() + */ + public static function date($format, $timestamp = NULL) { + return date($format, $timestamp ?: self::time()); + } + + /** + * Get the time. + * + * @return int + * seconds since epoch + * @see time() + */ + public static function time() { + return self::$callback === NULL ? time() : call_user_func(self::$callback); + } + /** * Get the time. * @@ -37,9 +81,11 @@ class CRM_Utils_Time { * Format in which date is to be retrieved. * * @return string + * @deprecated + * Prefer CRM_Utils_Time::date(), whose name looks similar to the stdlib work-a-like. */ public static function getTime($returnFormat = 'YmdHis') { - return date($returnFormat, self::getTimeRaw()); + return date($returnFormat, self::time()); } /** @@ -47,9 +93,11 @@ class CRM_Utils_Time { * * @return int * seconds since epoch + * @deprecated + * Prefer CRM_Utils_Time::time(), whose name looks similar to the stdlib work-a-like. */ public static function getTimeRaw() { - return self::$callback === NULL ? time() : call_user_func(self::$callback); + return self::time(); } /**