Merge pull request #15699 from mattwire/participant_cleanup_completeOrderPBRef
[civicrm-core.git] / CRM / Utils / Date.php
index e1fa77bcf4a0f86c902ecc73b887464e55f1c723..7ad11729ac63a13886111d2bb97ef9ebb5e16d25 100644 (file)
@@ -3,7 +3,7 @@
   +--------------------------------------------------------------------+
   | CiviCRM version 5                                                  |
   +--------------------------------------------------------------------+
-  | Copyright CiviCRM LLC (c) 2004-2019                                |
+  | Copyright CiviCRM LLC (c) 2004-2020                                |
   +--------------------------------------------------------------------+
   | This file is a part of CiviCRM.                                    |
   |                                                                    |
@@ -28,7 +28,7 @@
 /**
  *
  * @package CRM
- * @copyright CiviCRM LLC (c) 2004-2019
+ * @copyright CiviCRM LLC (c) 2004-2020
  */
 
 /**
@@ -176,7 +176,7 @@ class CRM_Utils_Date {
    *
    */
   public static function getAbbrWeekdayNames() {
-    static $days = array();
+    static $days = [];
     if (!$days) {
       // First day of the week
       $firstDay = Civi::settings()->get('weekBegins');
@@ -205,7 +205,7 @@ class CRM_Utils_Date {
    *
    */
   public static function getFullWeekdayNames() {
-    static $days = array();
+    static $days = [];
     if (!$days) {
       // First day of the week
       $firstDay = Civi::settings()->get('weekBegins');
@@ -322,13 +322,13 @@ class CRM_Utils_Date {
       $config = CRM_Core_Config::singleton();
 
       if ($dateParts) {
-        if (array_intersect(array('h', 'H'), $dateParts)) {
+        if (array_intersect(['h', 'H'], $dateParts)) {
           $format = $config->dateformatDatetime;
         }
-        elseif (array_intersect(array('d', 'j'), $dateParts)) {
+        elseif (array_intersect(['d', 'j'], $dateParts)) {
           $format = $config->dateformatFull;
         }
-        elseif (array_intersect(array('m', 'M'), $dateParts)) {
+        elseif (array_intersect(['m', 'M'], $dateParts)) {
           $format = $config->dateformatPartial;
         }
         else {
@@ -410,7 +410,7 @@ class CRM_Utils_Date {
         $type = 'PM';
       }
 
-      $date = array(
+      $date = [
         '%b' => CRM_Utils_Array::value($month, $abbrMonths),
         '%B' => CRM_Utils_Array::value($month, $fullMonths),
         '%d' => $day > 9 ? $day : '0' . $day,
@@ -429,7 +429,7 @@ class CRM_Utils_Date {
         '%P' => $type,
         '%A' => $type,
         '%Y' => $year,
-      );
+      ];
 
       return strtr($format, $date);
     }
@@ -508,7 +508,7 @@ class CRM_Utils_Date {
    *   date/datetime in MySQL format
    */
   public static function isoToMysql($iso) {
-    $dropArray = array('-' => '', ':' => '', ' ' => '');
+    $dropArray = ['-' => '', ':' => '', ' ' => ''];
     return strtr($iso, $dropArray);
   }
 
@@ -763,7 +763,6 @@ class CRM_Utils_Date {
     }
   }
 
-
   /**
    * @param null $timeStamp
    *
@@ -889,7 +888,7 @@ class CRM_Utils_Date {
     $from = self::processDate($from, $fromTime);
     $to = self::processDate($to, $toTime);
 
-    return array($from, $to);
+    return [$from, $to];
   }
 
   /**
@@ -901,8 +900,8 @@ class CRM_Utils_Date {
    * @return int
    *   array $results contains years or months
    */
-  static public function calculateAge($birthDate) {
-    $results = array();
+  public static function calculateAge($birthDate) {
+    $results = [];
     $formatedBirthDate = CRM_Utils_Date::customFormat($birthDate, '%Y-%m-%d');
 
     $bDate = explode('-', $formatedBirthDate);
@@ -1003,7 +1002,7 @@ class CRM_Utils_Date {
 
     $scheduleDate = explode("-", date("n-j-Y-H-i-s", $date));
 
-    $date = array();
+    $date = [];
     $date['M'] = $scheduleDate[0];
     $date['d'] = $scheduleDate[1];
     $date['Y'] = $scheduleDate[2];
@@ -1031,14 +1030,14 @@ class CRM_Utils_Date {
    *   according to the available parts at the smarty layer.
    */
   public static function getDateFieldViewFormat($format) {
-    $supportableFormats = array(
+    $supportableFormats = [
       'mm/dd' => '%B %E%f',
       'dd-mm' => '%E%f %B',
       'yy-mm' => '%Y %B',
       'M yy' => '%b %Y',
       'yy' => '%Y',
       'dd/mm/yy' => '%E%f %B %Y',
-    );
+    ];
 
     return array_key_exists($format, $supportableFormats) ? $supportableFormats[$format] : self::pickBestSmartyFormat($format);
   }
@@ -1071,7 +1070,7 @@ class CRM_Utils_Date {
    * @return array
    */
   public static function datePluginToPHPFormats() {
-    $dateInputFormats = array(
+    $dateInputFormats = [
       "mm/dd/yy" => 'm/d/Y',
       "dd/mm/yy" => 'd/m/Y',
       "yy-mm-dd" => 'Y-m-d',
@@ -1089,7 +1088,7 @@ class CRM_Utils_Date {
       "M yy" => 'M Y',
       "M Y" => 'M Y',
       "yy" => 'Y',
-    );
+    ];
     return $dateInputFormats;
   }
 
@@ -1106,7 +1105,7 @@ class CRM_Utils_Date {
    */
   public static function relativeToAbsolute($relativeTerm, $unit) {
     $now = getdate();
-    $from = $to = $dateRange = array();
+    $from = $to = $dateRange = [];
     $from['H'] = $from['i'] = $from['s'] = 0;
     $relativeTermParts = explode('_', $relativeTerm);
     $relativeTermPrefix = $relativeTermParts[0];
@@ -1115,13 +1114,6 @@ class CRM_Utils_Date {
     switch ($unit) {
       case 'year':
         switch ($relativeTerm) {
-          case 'this':
-            $from['d'] = $from['M'] = 1;
-            $to['d'] = 31;
-            $to['M'] = 12;
-            $to['Y'] = $from['Y'] = $now['year'];
-            break;
-
           case 'previous':
             $from['M'] = $from['d'] = 1;
             $to['d'] = 31;
@@ -1228,15 +1220,29 @@ class CRM_Utils_Date {
             break;
 
           default:
-            if ($relativeTermPrefix === 'ending') {
-              $to['d'] = $now['mday'];
-              $to['M'] = $now['mon'];
-              $to['Y'] = $now['year'];
-              $to['H'] = 23;
-              $to['i'] = $to['s'] = 59;
-              $from = self::intervalAdd('year', -$relativeTermSuffix, $to);
-              $from = self::intervalAdd('second', 1, $from);
+            switch ($relativeTermPrefix) {
+
+              case 'ending':
+                $to['d'] = $now['mday'];
+                $to['M'] = $now['mon'];
+                $to['Y'] = $now['year'];
+                $to['H'] = 23;
+                $to['i'] = $to['s'] = 59;
+                $from = self::intervalAdd('year', -$relativeTermSuffix, $to);
+                $from = self::intervalAdd('second', 1, $from);
+                break;
+
+              case 'this':
+                $from['d'] = $from['M'] = 1;
+                $to['d'] = 31;
+                $to['M'] = 12;
+                $to['Y'] = $from['Y'] = $now['year'];
+                if (is_numeric($relativeTermSuffix)) {
+                  $from['Y'] -= ($relativeTermSuffix - 1);
+                }
+                break;
             }
+            break;
         }
         break;
 
@@ -1250,10 +1256,15 @@ class CRM_Utils_Date {
             $from['Y'] = $fYear;
             $fiscalYear = mktime(0, 0, 0, $from['M'], $from['d'] - 1, $from['Y'] + 1);
             $fiscalEnd = explode('-', date("Y-m-d", $fiscalYear));
-
             $to['d'] = $fiscalEnd['2'];
             $to['M'] = $fiscalEnd['1'];
             $to['Y'] = $fiscalEnd['0'];
+            $to['H'] = 23;
+            $to['i'] = $to['s'] = 59;
+            if (is_numeric($relativeTermSuffix)) {
+              $from = self::intervalAdd('year', (-$relativeTermSuffix), $to);
+              $from = self::intervalAdd('second', 1, $from);
+            }
             break;
 
           case 'previous':
@@ -1264,6 +1275,8 @@ class CRM_Utils_Date {
               $to['d'] = $fiscalEnd['2'];
               $to['M'] = $fiscalEnd['1'];
               $to['Y'] = $fiscalEnd['0'];
+              $to['H'] = 23;
+              $to['i'] = $to['s'] = 59;
             }
             else {
               $from['Y'] = $fYear - $relativeTermSuffix;
@@ -1272,6 +1285,8 @@ class CRM_Utils_Date {
               $to['d'] = $fiscalEnd['2'];
               $to['M'] = $fiscalEnd['1'];
               $to['Y'] = $fYear;
+              $to['H'] = 23;
+              $to['i'] = $to['s'] = 59;
             }
             break;
 
@@ -1857,17 +1872,8 @@ class CRM_Utils_Date {
         break;
     }
 
-    foreach (array(
-               'from',
-               'to',
-             ) as $item) {
-      if (!empty($$item)) {
-        $dateRange[$item] = self::format($$item);
-      }
-      else {
-        $dateRange[$item] = NULL;
-      }
-    }
+    $dateRange['from'] = empty($from) ? NULL : self::format($from);
+    $dateRange['to'] = empty($to) ? NULL : self::format($to);
     return $dateRange;
   }
 
@@ -1961,7 +1967,6 @@ class CRM_Utils_Date {
     return $field;
   }
 
-
   /**
    * Get the fields required for the 'extra' parameter when adding a datepicker.
    *
@@ -1970,7 +1975,7 @@ class CRM_Utils_Date {
    * @return array
    */
   public static function getDatePickerExtra($field) {
-    $extra = array();
+    $extra = [];
     if (isset($field['date_format'])) {
       $extra['date'] = $field['date_format'];
       $extra['time'] = $field['time_format'];
@@ -1995,12 +2000,12 @@ class CRM_Utils_Date {
    *   Array ready to pass to $this->addForm('datepicker' as attributes.
    */
   public static function getDatePickerAttributes(&$field) {
-    $attributes = array();
-    $dateAttributes = array(
+    $attributes = [];
+    $dateAttributes = [
       'start_date_years' => 'minYear',
       'end_date_years' => 'maxYear',
       'date_format' => 'format',
-    );
+    ];
     foreach ($dateAttributes as $dateAttribute => $mapTo) {
       if (isset($field[$dateAttribute])) {
         $attributes[$mapTo] = $field[$dateAttribute];
@@ -2031,8 +2036,8 @@ class CRM_Utils_Date {
     $config = CRM_Core_Config::singleton();
     if ($formatType) {
       // get actual format
-      $params = array('name' => $formatType);
-      $values = array();
+      $params = ['name' => $formatType];
+      $values = [];
       CRM_Core_DAO::commonRetrieve('CRM_Core_DAO_PreferencesDate', $params, $values);
 
       if ($values['date_format']) {
@@ -2078,7 +2083,7 @@ class CRM_Utils_Date {
       $time = '0' . $time;
     }
 
-    return array($date, $time);
+    return [$date, $time];
   }
 
   /**
@@ -2122,13 +2127,13 @@ class CRM_Utils_Date {
     // 4. If date and time then convert to default date time format.
 
     $dateKey = 'date';
-    $dateParams = array($dateKey => $date);
+    $dateParams = [$dateKey => $date];
 
     if (CRM_Utils_Date::convertToDefaultDate($dateParams, $dateType, $dateKey)) {
       $dateVal = $dateParams[$dateKey];
       $ruleName = 'date';
       if ($dateType == 1) {
-        $matches = array();
+        $matches = [];
         if (preg_match("/(\s(([01]\d)|[2][0-3]):([0-5]\d))$/", $date, $matches)) {
           $ruleName = 'dateTime';
           if (strpos($date, '-') !== FALSE) {
@@ -2160,7 +2165,7 @@ class CRM_Utils_Date {
    * @return array
    */
   public static function getCalendarDayOfMonth() {
-    $month = array();
+    $month = [];
     for ($i = 1; $i <= 31; $i++) {
       $month[$i] = $i;
       if ($i == 31) {
@@ -2170,7 +2175,6 @@ class CRM_Utils_Date {
     return $month;
   }
 
-
   /**
    * Convert a relative date format to an api field.
    *
@@ -2207,4 +2211,17 @@ class CRM_Utils_Date {
     }
   }
 
+  /**
+   * Print out a date object in specified format in local timezone
+   *
+   * @param DateTimeObject $dateObject
+   * @param string $format
+   * @return string
+   */
+  public static function convertDateToLocalTime($dateObject, $format = 'YmdHis') {
+    $systemTimeZone = new DateTimeZone(CRM_Core_Config::singleton()->userSystem->getTimeZoneString());
+    $dateObject->setTimezone($systemTimeZone);
+    return $dateObject->format($format);
+  }
+
 }