Merge pull request #15909 from eileenmcnaughton/payment
[civicrm-core.git] / CRM / Utils / Date.php
index fa96fca47fa1a0394a2bd4cd7b6c781a3101d498..f5beabb2622cfa2f870ae1e90e0bfcda567dd927 100644 (file)
@@ -1,34 +1,18 @@
 <?php
 /*
-  +--------------------------------------------------------------------+
-  | CiviCRM version 5                                                  |
-  +--------------------------------------------------------------------+
-  | Copyright CiviCRM LLC (c) 2004-2019                                |
-  +--------------------------------------------------------------------+
-  | This file is a part of CiviCRM.                                    |
-  |                                                                    |
-  | CiviCRM is free software; you can copy, modify, and distribute it  |
-  | under the terms of the GNU Affero General Public License           |
-  | Version 3, 19 November 2007 and the CiviCRM Licensing Exception.   |
-  |                                                                    |
-  | CiviCRM is distributed in the hope that it will be useful, but     |
-  | WITHOUT ANY WARRANTY; without even the implied warranty of         |
-  | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.               |
-  | See the GNU Affero General Public License for more details.        |
-  |                                                                    |
-  | You should have received a copy of the GNU Affero General Public   |
-  | License and the CiviCRM Licensing Exception along                  |
-  | with this program; if not, contact CiviCRM LLC                     |
-  | at info[AT]civicrm[DOT]org. If you have questions about the        |
-  | GNU Affero General Public License or the licensing of CiviCRM,     |
-  | see the CiviCRM license FAQ at http://civicrm.org/licensing        |
-  +--------------------------------------------------------------------+
+ +--------------------------------------------------------------------+
+ | Copyright CiviCRM LLC. All rights reserved.                        |
+ |                                                                    |
+ | This work is published under the GNU AGPLv3 license with some      |
+ | permitted exceptions and without any warranty. For full license    |
+ | and copyright information, see https://civicrm.org/licensing       |
+ +--------------------------------------------------------------------+
  */
 
 /**
  *
  * @package CRM
- * @copyright CiviCRM LLC (c) 2004-2019
+ * @copyright CiviCRM LLC https://civicrm.org/licensing
  */
 
 /**
@@ -1114,13 +1098,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;
@@ -1227,15 +1204,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;
 
@@ -1249,10 +1240,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':
@@ -1263,6 +1259,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;
@@ -1271,6 +1269,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;
 
@@ -1856,17 +1856,8 @@ class CRM_Utils_Date {
         break;
     }
 
-    foreach ([
-      '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;
   }