| 1441 |
ariadna |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
namespace PhpOffice\PhpSpreadsheet\Calculation\DateTimeExcel;
|
|
|
4 |
|
|
|
5 |
use DateInterval;
|
|
|
6 |
use DateTime;
|
|
|
7 |
use PhpOffice\PhpSpreadsheet\Calculation\ArrayEnabled;
|
|
|
8 |
use PhpOffice\PhpSpreadsheet\Calculation\Exception;
|
|
|
9 |
use PhpOffice\PhpSpreadsheet\Calculation\Information\ExcelError;
|
|
|
10 |
use 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|string $unit Or can be an array of unit values
|
|
|
26 |
*
|
|
|
27 |
* @return array|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(mixed $startDate, mixed $endDate, array|string $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 |
*/
|
|
|
83 |
private static function replaceRetValue(bool|int $retVal, string $unit, string $compare): null|bool|int
|
|
|
84 |
{
|
|
|
85 |
if ($retVal !== false || $unit !== $compare) {
|
|
|
86 |
return $retVal;
|
|
|
87 |
}
|
|
|
88 |
|
|
|
89 |
return null;
|
|
|
90 |
}
|
|
|
91 |
|
|
|
92 |
private static function datedifD(float $difference): int
|
|
|
93 |
{
|
|
|
94 |
return (int) $difference;
|
|
|
95 |
}
|
|
|
96 |
|
|
|
97 |
private static function datedifM(DateInterval $PHPDiffDateObject): int
|
|
|
98 |
{
|
|
|
99 |
return 12 * (int) $PHPDiffDateObject->format('%y') + (int) $PHPDiffDateObject->format('%m');
|
|
|
100 |
}
|
|
|
101 |
|
|
|
102 |
private static function datedifMD(int $startDays, int $endDays, DateTime $PHPEndDateObject, DateInterval $PHPDiffDateObject): int
|
|
|
103 |
{
|
|
|
104 |
if ($endDays < $startDays) {
|
|
|
105 |
$retVal = $endDays;
|
|
|
106 |
$PHPEndDateObject->modify('-' . $endDays . ' days');
|
|
|
107 |
$adjustDays = (int) $PHPEndDateObject->format('j');
|
|
|
108 |
$retVal += ($adjustDays - $startDays);
|
|
|
109 |
} else {
|
|
|
110 |
$retVal = (int) $PHPDiffDateObject->format('%d');
|
|
|
111 |
}
|
|
|
112 |
|
|
|
113 |
return $retVal;
|
|
|
114 |
}
|
|
|
115 |
|
|
|
116 |
private static function datedifY(DateInterval $PHPDiffDateObject): int
|
|
|
117 |
{
|
|
|
118 |
return (int) $PHPDiffDateObject->format('%y');
|
|
|
119 |
}
|
|
|
120 |
|
|
|
121 |
private static function datedifYD(float $difference, int $startYears, int $endYears, DateTime $PHPStartDateObject, DateTime $PHPEndDateObject): int
|
|
|
122 |
{
|
|
|
123 |
$retVal = (int) $difference;
|
|
|
124 |
if ($endYears > $startYears) {
|
|
|
125 |
$isLeapStartYear = $PHPStartDateObject->format('L');
|
|
|
126 |
$wasLeapEndYear = $PHPEndDateObject->format('L');
|
|
|
127 |
|
|
|
128 |
// Adjust end year to be as close as possible as start year
|
|
|
129 |
while ($PHPEndDateObject >= $PHPStartDateObject) {
|
|
|
130 |
$PHPEndDateObject->modify('-1 year');
|
|
|
131 |
//$endYears = $PHPEndDateObject->format('Y');
|
|
|
132 |
}
|
|
|
133 |
$PHPEndDateObject->modify('+1 year');
|
|
|
134 |
|
|
|
135 |
// Get the result
|
|
|
136 |
$retVal = (int) $PHPEndDateObject->diff($PHPStartDateObject)->days;
|
|
|
137 |
|
|
|
138 |
// Adjust for leap years cases
|
|
|
139 |
$isLeapEndYear = $PHPEndDateObject->format('L');
|
|
|
140 |
$limit = new DateTime($PHPEndDateObject->format('Y-02-29'));
|
|
|
141 |
if (!$isLeapStartYear && !$wasLeapEndYear && $isLeapEndYear && $PHPEndDateObject >= $limit) {
|
|
|
142 |
--$retVal;
|
|
|
143 |
}
|
|
|
144 |
}
|
|
|
145 |
|
|
|
146 |
return (int) $retVal;
|
|
|
147 |
}
|
|
|
148 |
|
|
|
149 |
private static function datedifYM(DateInterval $PHPDiffDateObject): int
|
|
|
150 |
{
|
|
|
151 |
return (int) $PHPDiffDateObject->format('%m');
|
|
|
152 |
}
|
|
|
153 |
}
|