| 1 |
<?php |
| 2 |
|
| 3 |
namespace TablePress\PhpOffice\PhpSpreadsheet\Calculation\DateTimeExcel; |
| 4 |
|
| 5 |
use DateInterval; |
| 6 |
use DateTime; |
| 7 |
use TablePress\PhpOffice\PhpSpreadsheet\Calculation\ArrayEnabled; |
| 8 |
use TablePress\PhpOffice\PhpSpreadsheet\Calculation\Exception; |
| 9 |
use TablePress\PhpOffice\PhpSpreadsheet\Calculation\Information\ExcelError; |
| 10 |
use TablePress\PhpOffice\PhpSpreadsheet\Shared\Date as SharedDateHelper; |
| 11 |
|
| 12 |
class Difference |
| 13 |
{ |
| 14 |
use ArrayEnabled; |
| 15 |
|
| 16 |
/** |
| 17 |
* DATEDIF. |
| 18 |
* |
| 19 |
* @param mixed $startDate Excel date serial value, PHP date/time stamp, PHP DateTime object |
| 20 |
* or a standard date string |
| 21 |
* Or can be an array of date values |
| 22 |
* @param mixed $endDate Excel date serial value, PHP date/time stamp, PHP DateTime object |
| 23 |
* or a standard date string |
| 24 |
* Or can be an array of date values |
| 25 |
* @param array<mixed>|string $unit Or can be an array of unit values |
| 26 |
* |
| 27 |
* @return array<mixed>|int|string Interval between the dates |
| 28 |
* If an array of values is passed for the $startDate or $endDays,arguments, then the returned result |
| 29 |
* will also be an array with matching dimensions |
| 30 |
*/ |
| 31 |
public static function interval($startDate, $endDate, $unit = 'D') |
| 32 |
{ |
| 33 |
if (is_array($startDate) || is_array($endDate) || is_array($unit)) { |
| 34 |
return self::evaluateArrayArguments([self::class, __FUNCTION__], $startDate, $endDate, $unit); |
| 35 |
} |
| 36 |
|
| 37 |
try { |
| 38 |
$startDate = Helpers::getDateValue($startDate); |
| 39 |
$endDate = Helpers::getDateValue($endDate); |
| 40 |
$difference = self::initialDiff($startDate, $endDate); |
| 41 |
$unit = strtoupper($unit); |
| 42 |
} catch (Exception $e) { |
| 43 |
return $e->getMessage(); |
| 44 |
} |
| 45 |
|
| 46 |
// Execute function |
| 47 |
$PHPStartDateObject = SharedDateHelper::excelToDateTimeObject($startDate); |
| 48 |
$startDays = (int) $PHPStartDateObject->format('j'); |
| 49 |
//$startMonths = (int) $PHPStartDateObject->format('n'); |
| 50 |
$startYears = (int) $PHPStartDateObject->format('Y'); |
| 51 |
|
| 52 |
$PHPEndDateObject = SharedDateHelper::excelToDateTimeObject($endDate); |
| 53 |
$endDays = (int) $PHPEndDateObject->format('j'); |
| 54 |
//$endMonths = (int) $PHPEndDateObject->format('n'); |
| 55 |
$endYears = (int) $PHPEndDateObject->format('Y'); |
| 56 |
|
| 57 |
$PHPDiffDateObject = $PHPEndDateObject->diff($PHPStartDateObject); |
| 58 |
|
| 59 |
$retVal = false; |
| 60 |
$retVal = self::replaceRetValue($retVal, $unit, 'D') ?? self::datedifD($difference); |
| 61 |
$retVal = self::replaceRetValue($retVal, $unit, 'M') ?? self::datedifM($PHPDiffDateObject); |
| 62 |
$retVal = self::replaceRetValue($retVal, $unit, 'MD') ?? self::datedifMD($startDays, $endDays, $PHPEndDateObject, $PHPDiffDateObject); |
| 63 |
$retVal = self::replaceRetValue($retVal, $unit, 'Y') ?? self::datedifY($PHPDiffDateObject); |
| 64 |
$retVal = self::replaceRetValue($retVal, $unit, 'YD') ?? self::datedifYD($difference, $startYears, $endYears, $PHPStartDateObject, $PHPEndDateObject); |
| 65 |
$retVal = self::replaceRetValue($retVal, $unit, 'YM') ?? self::datedifYM($PHPDiffDateObject); |
| 66 |
|
| 67 |
return is_bool($retVal) ? ExcelError::VALUE() : $retVal; |
| 68 |
} |
| 69 |
|
| 70 |
private static function initialDiff(float $startDate, float $endDate): float |
| 71 |
{ |
| 72 |
// Validate parameters |
| 73 |
if ($startDate > $endDate) { |
| 74 |
throw new Exception(ExcelError::NAN()); |
| 75 |
} |
| 76 |
|
| 77 |
return $endDate - $startDate; |
| 78 |
} |
| 79 |
|
| 80 |
/** |
| 81 |
* Decide whether it's time to set retVal. |
| 82 |
* @param bool|int $retVal |
| 83 |
* @return null|bool|int |
| 84 |
*/ |
| 85 |
private static function replaceRetValue($retVal, string $unit, string $compare) |
| 86 |
{ |
| 87 |
if ($retVal !== false || $unit !== $compare) { |
| 88 |
return $retVal; |
| 89 |
} |
| 90 |
|
| 91 |
return null; |
| 92 |
} |
| 93 |
|
| 94 |
private static function datedifD(float $difference): int |
| 95 |
{ |
| 96 |
return (int) $difference; |
| 97 |
} |
| 98 |
|
| 99 |
private static function datedifM(DateInterval $PHPDiffDateObject): int |
| 100 |
{ |
| 101 |
return 12 * (int) $PHPDiffDateObject->format('%y') + (int) $PHPDiffDateObject->format('%m'); |
| 102 |
} |
| 103 |
|
| 104 |
private static function datedifMD(int $startDays, int $endDays, DateTime $PHPEndDateObject, DateInterval $PHPDiffDateObject): int |
| 105 |
{ |
| 106 |
if ($endDays < $startDays) { |
| 107 |
$retVal = $endDays; |
| 108 |
$PHPEndDateObject->modify('-' . $endDays . ' days'); |
| 109 |
$adjustDays = (int) $PHPEndDateObject->format('j'); |
| 110 |
$retVal += ($adjustDays - $startDays); |
| 111 |
} else { |
| 112 |
$retVal = (int) $PHPDiffDateObject->format('%d'); |
| 113 |
} |
| 114 |
|
| 115 |
return $retVal; |
| 116 |
} |
| 117 |
|
| 118 |
private static function datedifY(DateInterval $PHPDiffDateObject): int |
| 119 |
{ |
| 120 |
return (int) $PHPDiffDateObject->format('%y'); |
| 121 |
} |
| 122 |
|
| 123 |
private static function datedifYD(float $difference, int $startYears, int $endYears, DateTime $PHPStartDateObject, DateTime $PHPEndDateObject): int |
| 124 |
{ |
| 125 |
$retVal = (int) $difference; |
| 126 |
if ($endYears > $startYears) { |
| 127 |
$isLeapStartYear = $PHPStartDateObject->format('L'); |
| 128 |
$wasLeapEndYear = $PHPEndDateObject->format('L'); |
| 129 |
|
| 130 |
// Adjust end year to be as close as possible as start year |
| 131 |
while ($PHPEndDateObject >= $PHPStartDateObject) { |
| 132 |
$PHPEndDateObject->modify('-1 year'); |
| 133 |
//$endYears = $PHPEndDateObject->format('Y'); |
| 134 |
} |
| 135 |
$PHPEndDateObject->modify('+1 year'); |
| 136 |
|
| 137 |
// Get the result |
| 138 |
$retVal = (int) $PHPEndDateObject->diff($PHPStartDateObject)->days; |
| 139 |
|
| 140 |
// Adjust for leap years cases |
| 141 |
$isLeapEndYear = $PHPEndDateObject->format('L'); |
| 142 |
$limit = new DateTime($PHPEndDateObject->format('Y-02-29')); |
| 143 |
if (!$isLeapStartYear && !$wasLeapEndYear && $isLeapEndYear && $PHPEndDateObject >= $limit) { |
| 144 |
--$retVal; |
| 145 |
} |
| 146 |
} |
| 147 |
|
| 148 |
return (int) $retVal; |
| 149 |
} |
| 150 |
|
| 151 |
private static function datedifYM(DateInterval $PHPDiffDateObject): int |
| 152 |
{ |
| 153 |
return (int) $PHPDiffDateObject->format('%m'); |
| 154 |
} |
| 155 |
} |
| 156 |
|