Skip to content

Commit d5b37a9

Browse files
committed
Handle 61 days ago as 2 months
We have a fun problem with the summer months, where July and August has 31 days each. This resulted in the script returning "1 months ago" instead of 2. The DateTime diff() was not incorrect though, as it was only 1 month and 31 days ago, but for our purpose, i have changed the script to round up from 1 to 2, when we are about 59days ago. Handles jimmiw#95
1 parent 2e0da84 commit d5b37a9

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

src/Westsworld/TimeAgo/Language.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -378,7 +378,7 @@ private function roundMonthsAboveOneMonth(DateInterval $timeDifference)
378378
// $months = 2;
379379
// }
380380
// return $months;
381-
return (int)$timeDifference->m;
381+
return (int)($timeDifference->m === 1 ? 2 : $timeDifference->m);
382382
}
383383

384384
/**

tests/TimeagoTest.php

+14
Original file line numberDiff line numberDiff line change
@@ -160,4 +160,18 @@ public function testLanguage()
160160
$timeAgo = new TimeAgo(new \Westsworld\TimeAgo\Translations\Sv_SE());
161161
$this->assertEquals('mindre än en minut sedan', $timeAgo->inWords(new DateTime()));
162162
}
163+
164+
/**
165+
* An issue with "1 mounts ago" was raised from users of the module.
166+
* StartDate: 01 July 2022
167+
* EndDate: 01 September 2022
168+
* Result with ->inWords(): "1 months ago"
169+
*/
170+
public function testIssue95()
171+
{
172+
$timeAgo = new TimeAgo();
173+
$startDate = new DateTime('2022-07-01 15:00:00');
174+
$endDate = new DateTime('2022-09-01 14:00:00');
175+
$this->assertEquals('2 months ago', $timeAgo->inWords($startDate, $endDate));
176+
}
163177
}

0 commit comments

Comments
 (0)