Override the DrupalBase getUserObject function with a Drupal8/9 compatible version
[civicrm-core.git] / CRM / Utils / Time.php
index b700e572071092ee7c9f1462b914613b876c406f..dd56d6b9e422125c1c3832cbbf6b521e6b3c87b5 100644 (file)
@@ -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();
   }
 
   /**