Calcualtion - DATEDIF - fix result for Y & YM units (#1466)
Bugfix for negative results and too small results 2000-02-02 => 2001-02-01 > DATEDIF with Y unit: 0 year (returned -1 before fix) > DATEDIF with YM unit: 11 months (returned -1 before fix)
This commit is contained in:
parent
5a92a5f6b4
commit
7ed96e0be1
|
@ -668,30 +668,19 @@ class DateTime
|
||||||
$endMonths = $PHPEndDateObject->format('n');
|
$endMonths = $PHPEndDateObject->format('n');
|
||||||
$endYears = $PHPEndDateObject->format('Y');
|
$endYears = $PHPEndDateObject->format('Y');
|
||||||
|
|
||||||
|
$PHPDiffDateObject = $PHPEndDateObject->diff($PHPStartDateObject);
|
||||||
|
|
||||||
switch ($unit) {
|
switch ($unit) {
|
||||||
case 'D':
|
case 'D':
|
||||||
$retVal = (int) $difference;
|
$retVal = (int) $difference;
|
||||||
|
|
||||||
break;
|
break;
|
||||||
case 'M':
|
case 'M':
|
||||||
$retVal = (int) ($endMonths - $startMonths) + ((int) ($endYears - $startYears) * 12);
|
$retVal = (int) 12 * $PHPDiffDateObject->format('%y') + $PHPDiffDateObject->format('%m');
|
||||||
// We're only interested in full months
|
|
||||||
if ($endDays < $startDays) {
|
|
||||||
--$retVal;
|
|
||||||
}
|
|
||||||
|
|
||||||
break;
|
break;
|
||||||
case 'Y':
|
case 'Y':
|
||||||
$retVal = (int) ($endYears - $startYears);
|
$retVal = (int) $PHPDiffDateObject->format('%y');
|
||||||
// We're only interested in full months
|
|
||||||
if ($endMonths < $startMonths) {
|
|
||||||
--$retVal;
|
|
||||||
} elseif (($endMonths == $startMonths) && ($endDays < $startDays)) {
|
|
||||||
// Remove start month
|
|
||||||
--$retVal;
|
|
||||||
// Remove end month
|
|
||||||
--$retVal;
|
|
||||||
}
|
|
||||||
|
|
||||||
break;
|
break;
|
||||||
case 'MD':
|
case 'MD':
|
||||||
|
@ -701,19 +690,12 @@ class DateTime
|
||||||
$adjustDays = $PHPEndDateObject->format('j');
|
$adjustDays = $PHPEndDateObject->format('j');
|
||||||
$retVal += ($adjustDays - $startDays);
|
$retVal += ($adjustDays - $startDays);
|
||||||
} else {
|
} else {
|
||||||
$retVal = $endDays - $startDays;
|
$retVal = (int) $PHPDiffDateObject->format('%d');
|
||||||
}
|
}
|
||||||
|
|
||||||
break;
|
break;
|
||||||
case 'YM':
|
case 'YM':
|
||||||
$retVal = (int) ($endMonths - $startMonths);
|
$retVal = (int) $PHPDiffDateObject->format('%m');
|
||||||
if ($retVal < 0) {
|
|
||||||
$retVal += 12;
|
|
||||||
}
|
|
||||||
// We're only interested in full months
|
|
||||||
if ($endDays < $startDays) {
|
|
||||||
--$retVal;
|
|
||||||
}
|
|
||||||
|
|
||||||
break;
|
break;
|
||||||
case 'YD':
|
case 'YD':
|
||||||
|
|
|
@ -393,6 +393,10 @@ return [
|
||||||
1,
|
1,
|
||||||
'19-12-1960', '26-01-2012', 'YM',
|
'19-12-1960', '26-01-2012', 'YM',
|
||||||
],
|
],
|
||||||
|
[
|
||||||
|
11,
|
||||||
|
'19-12-1960', '26-11-1962', 'YM',
|
||||||
|
],
|
||||||
[
|
[
|
||||||
38,
|
38,
|
||||||
'19-12-1960', '26-01-2012', 'YD',
|
'19-12-1960', '26-01-2012', 'YD',
|
||||||
|
@ -402,7 +406,15 @@ return [
|
||||||
'19-12-1960', '26-01-2012', 'MD',
|
'19-12-1960', '26-01-2012', 'MD',
|
||||||
],
|
],
|
||||||
[
|
[
|
||||||
50,
|
0,
|
||||||
|
'19-12-1960', '12-12-1961', 'Y',
|
||||||
|
],
|
||||||
|
[
|
||||||
|
1,
|
||||||
|
'19-12-1960', '12-12-1962', 'Y',
|
||||||
|
],
|
||||||
|
[
|
||||||
|
51,
|
||||||
'19-12-1960', '12-12-2012', 'Y',
|
'19-12-1960', '12-12-2012', 'Y',
|
||||||
],
|
],
|
||||||
[
|
[
|
||||||
|
|
Loading…
Reference in New Issue