From feac14ca9ae3d144c4647003f23577fd80379234 Mon Sep 17 00:00:00 2001 From: mscherer Date: Sat, 7 Feb 2026 17:15:55 +0100 Subject: [PATCH] Return 'just now' for zero-second differences When diffForHumans() is called on two identical timestamps, return "just now" instead of the awkward "0 seconds ago". This provides a more natural, human-readable output for edge cases where two times are effectively the same. --- src/DifferenceFormatter.php | 5 +++++ src/Translator.php | 1 + tests/TestCase/Date/DiffTest.php | 4 ++-- tests/TestCase/DateTime/DiffTest.php | 4 ++-- 4 files changed, 10 insertions(+), 4 deletions(-) diff --git a/src/DifferenceFormatter.php b/src/DifferenceFormatter.php index 3a568c7..b3a4460 100644 --- a/src/DifferenceFormatter.php +++ b/src/DifferenceFormatter.php @@ -96,6 +96,11 @@ public function diffForHumans( $unit = 'second'; break; } + + if ($count === 0 && $unit === 'second') { + return $this->translate->singular('just_now'); + } + $time = $this->translate->plural($unit, $count, ['count' => $count]); if ($absolute) { return $time; diff --git a/src/Translator.php b/src/Translator.php index 37568ae..42c768c 100644 --- a/src/Translator.php +++ b/src/Translator.php @@ -40,6 +40,7 @@ class Translator 'minute_plural' => '{count} minutes', 'second' => '1 second', 'second_plural' => '{count} seconds', + 'just_now' => 'just now', 'ago' => '{time} ago', 'from_now' => '{time} from now', 'after' => '{time} after', diff --git a/tests/TestCase/Date/DiffTest.php b/tests/TestCase/Date/DiffTest.php index e78b540..3ce4b70 100644 --- a/tests/TestCase/Date/DiffTest.php +++ b/tests/TestCase/Date/DiffTest.php @@ -350,14 +350,14 @@ public function testDiffForHumansWithNowAbsolute() public function testDiffForHumansWithoutDiff() { $this->wrapWithTestNow(function () { - $this->assertSame('0 seconds ago', ChronosDate::parse(Chronos::now())->diffForHumans()); + $this->assertSame('just now', ChronosDate::parse(Chronos::now())->diffForHumans()); }); } public function testDiffForHumansWithoutDiffAbsolute() { $this->wrapWithTestNow(function () { - $this->assertSame('0 seconds', ChronosDate::parse(Chronos::now())->diffForHumans(null, true)); + $this->assertSame('just now', ChronosDate::parse(Chronos::now())->diffForHumans(null, true)); }); } } diff --git a/tests/TestCase/DateTime/DiffTest.php b/tests/TestCase/DateTime/DiffTest.php index 90abb7e..1f58adf 100644 --- a/tests/TestCase/DateTime/DiffTest.php +++ b/tests/TestCase/DateTime/DiffTest.php @@ -612,14 +612,14 @@ public function testDiffForHumansWithNowAbsolute() public function testDiffForHumansWithoutDiff() { $this->wrapWithTestNow(function () { - $this->assertSame('0 seconds ago', Chronos::now()->diffForHumans()); + $this->assertSame('just now', Chronos::now()->diffForHumans()); }); } public function testDiffForHumansWithoutDiffAbsolute() { $this->wrapWithTestNow(function () { - $this->assertSame('0 seconds', Chronos::now()->diffForHumans(null, true)); + $this->assertSame('just now', Chronos::now()->diffForHumans(null, true)); }); }