<?php
/*
+--------------------------------------------------------------------+
- | CiviCRM version 4.7 |
+ | CiviCRM version 5 |
+--------------------------------------------------------------------+
| Copyright CiviCRM LLC (c) 2004-2018 |
+--------------------------------------------------------------------+
/**
* Resolves the given relative time interval into finite time limits.
*
- * @param array $relativeTerm
- * Relative time frame like this, previous, etc.
+ * @param string $relativeTerm
+ * Relative time frame: this, previous, previous_1.
* @param int $unit
* Frequency unit like year, month, week etc.
*
$now = getdate();
$from = $to = $dateRange = array();
$from['H'] = $from['i'] = $from['s'] = 0;
+ $relativeTermParts = explode('_', $relativeTerm);
+ $relativeTermPrefix = $relativeTermParts[0];
+ $relativeTermSuffix = isset($relativeTermParts[1]) ? $relativeTermParts[1] : '';
switch ($unit) {
case 'year':
$from['d'] = $config->fiscalYearStart['d'];
$from['M'] = $config->fiscalYearStart['M'];
$fYear = self::calculateFiscalYear($from['d'], $from['M']);
- switch ($relativeTerm) {
+ switch ($relativeTermPrefix) {
case 'this':
$from['Y'] = $fYear;
$fiscalYear = mktime(0, 0, 0, $from['M'], $from['d'] - 1, $from['Y'] + 1);
break;
case 'previous':
- $from['Y'] = $fYear - 1;
- $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'];
+ if (!is_numeric($relativeTermSuffix)) {
+ $from['Y'] = ($relativeTermSuffix === 'before') ? $fYear - 2 : $fYear - 1;
+ $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'];
+ }
+ else {
+ $from['Y'] = $fYear - $relativeTermSuffix;
+ $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'] = $fYear;
+ }
break;
case 'next':
* Fiscal Start Month.
*
* @return int
- * $fy Current Fiscl Year
+ * $fy Current Fiscal Year
*/
public static function calculateFiscalYear($fyDate, $fyMonth) {
$date = date("Y-m-d");