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');
|
||||
$endYears = $PHPEndDateObject->format('Y');
|
||||
|
||||
$PHPDiffDateObject = $PHPEndDateObject->diff($PHPStartDateObject);
|
||||
|
||||
switch ($unit) {
|
||||
case 'D':
|
||||
$retVal = (int) $difference;
|
||||
|
||||
break;
|
||||
case 'M':
|
||||
$retVal = (int) ($endMonths - $startMonths) + ((int) ($endYears - $startYears) * 12);
|
||||
// We're only interested in full months
|
||||
if ($endDays < $startDays) {
|
||||
--$retVal;
|
||||
}
|
||||
$retVal = (int) 12 * $PHPDiffDateObject->format('%y') + $PHPDiffDateObject->format('%m');
|
||||
|
||||
break;
|
||||
case 'Y':
|
||||
$retVal = (int) ($endYears - $startYears);
|
||||
// We're only interested in full months
|
||||
if ($endMonths < $startMonths) {
|
||||
--$retVal;
|
||||
} elseif (($endMonths == $startMonths) && ($endDays < $startDays)) {
|
||||
// Remove start month
|
||||
--$retVal;
|
||||
// Remove end month
|
||||
--$retVal;
|
||||
}
|
||||
$retVal = (int) $PHPDiffDateObject->format('%y');
|
||||
|
||||
break;
|
||||
case 'MD':
|
||||
|
@ -701,19 +690,12 @@ class DateTime
|
|||
$adjustDays = $PHPEndDateObject->format('j');
|
||||
$retVal += ($adjustDays - $startDays);
|
||||
} else {
|
||||
$retVal = $endDays - $startDays;
|
||||
$retVal = (int) $PHPDiffDateObject->format('%d');
|
||||
}
|
||||
|
||||
break;
|
||||
case 'YM':
|
||||
$retVal = (int) ($endMonths - $startMonths);
|
||||
if ($retVal < 0) {
|
||||
$retVal += 12;
|
||||
}
|
||||
// We're only interested in full months
|
||||
if ($endDays < $startDays) {
|
||||
--$retVal;
|
||||
}
|
||||
$retVal = (int) $PHPDiffDateObject->format('%m');
|
||||
|
||||
break;
|
||||
case 'YD':
|
||||
|
|
|
@ -393,6 +393,10 @@ return [
|
|||
1,
|
||||
'19-12-1960', '26-01-2012', 'YM',
|
||||
],
|
||||
[
|
||||
11,
|
||||
'19-12-1960', '26-11-1962', 'YM',
|
||||
],
|
||||
[
|
||||
38,
|
||||
'19-12-1960', '26-01-2012', 'YD',
|
||||
|
@ -402,7 +406,15 @@ return [
|
|||
'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',
|
||||
],
|
||||
[
|
||||
|
|
Loading…
Reference in New Issue