From 3e01a06de097fbd2880ee785349902bbc644e39e Mon Sep 17 00:00:00 2001 From: KyleKatarn Date: Sat, 12 Feb 2022 11:32:11 +0100 Subject: [PATCH] Fix ceil diff with short month --- src/Carbon/CarbonInterval.php | 8 +++++++- tests/CarbonInterval/ForHumansTest.php | 13 +++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/src/Carbon/CarbonInterval.php b/src/Carbon/CarbonInterval.php index 3ce70193cc..d465beac70 100644 --- a/src/Carbon/CarbonInterval.php +++ b/src/Carbon/CarbonInterval.php @@ -1654,8 +1654,14 @@ public function forHumans($syntax = null, $short = false, $parts = -1, $options \count($intervalValues->getNonZeroValues()) > $parts && ($count = \count($keys = array_keys($intervalValues->getValuesSequence()))) > 1 ) { + $index = min($count, $previousCount - 1) - 2; + + if ($index < 0) { + break; + } + $intervalValues = $this->copy()->roundUnit( - $keys[min($count, $previousCount - 1) - 2], + $keys[$index], 1, $method ); diff --git a/tests/CarbonInterval/ForHumansTest.php b/tests/CarbonInterval/ForHumansTest.php index 11de23266a..8179f60066 100644 --- a/tests/CarbonInterval/ForHumansTest.php +++ b/tests/CarbonInterval/ForHumansTest.php @@ -392,6 +392,19 @@ public function testRoundCarryOverDoesntMatter() $this->assertSame('3 months', $interval->forHumans(['parts' => 1, 'options' => CarbonInterface::CEIL])); } + public function testCeilShortMonth() + { + Carbon::setTestNow('2022-02-08T10:27:03Z'); + $this->assertSame( + '4 weeks ago', + Carbon::parse('2022-01-11 15:36:29')->diffForHumans(['parts' => 1, 'options' => Carbon::CEIL]) + ); + $this->assertSame( + '1 month ago', + Carbon::parse('2022-01-10 16:57:38')->diffForHumans(['parts' => 1, 'options' => Carbon::CEIL]) + ); + } + public function testSkipUnits() { CarbonInterval::setLocale('en');