Skip to content

Commit 1bafb4c

Browse files
authored
Return 'just now' for zero-second differences (#500)
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.
1 parent 31aa950 commit 1bafb4c

File tree

4 files changed

+10
-4
lines changed

4 files changed

+10
-4
lines changed

src/DifferenceFormatter.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,11 @@ public function diffForHumans(
9696
$unit = 'second';
9797
break;
9898
}
99+
100+
if ($count === 0 && $unit === 'second') {
101+
return $this->translate->singular('just_now');
102+
}
103+
99104
$time = $this->translate->plural($unit, $count, ['count' => $count]);
100105
if ($absolute) {
101106
return $time;

src/Translator.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ class Translator
4040
'minute_plural' => '{count} minutes',
4141
'second' => '1 second',
4242
'second_plural' => '{count} seconds',
43+
'just_now' => 'just now',
4344
'ago' => '{time} ago',
4445
'from_now' => '{time} from now',
4546
'after' => '{time} after',

tests/TestCase/Date/DiffTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -350,14 +350,14 @@ public function testDiffForHumansWithNowAbsolute()
350350
public function testDiffForHumansWithoutDiff()
351351
{
352352
$this->wrapWithTestNow(function () {
353-
$this->assertSame('0 seconds ago', ChronosDate::parse(Chronos::now())->diffForHumans());
353+
$this->assertSame('just now', ChronosDate::parse(Chronos::now())->diffForHumans());
354354
});
355355
}
356356

357357
public function testDiffForHumansWithoutDiffAbsolute()
358358
{
359359
$this->wrapWithTestNow(function () {
360-
$this->assertSame('0 seconds', ChronosDate::parse(Chronos::now())->diffForHumans(null, true));
360+
$this->assertSame('just now', ChronosDate::parse(Chronos::now())->diffForHumans(null, true));
361361
});
362362
}
363363
}

tests/TestCase/DateTime/DiffTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -612,14 +612,14 @@ public function testDiffForHumansWithNowAbsolute()
612612
public function testDiffForHumansWithoutDiff()
613613
{
614614
$this->wrapWithTestNow(function () {
615-
$this->assertSame('0 seconds ago', Chronos::now()->diffForHumans());
615+
$this->assertSame('just now', Chronos::now()->diffForHumans());
616616
});
617617
}
618618

619619
public function testDiffForHumansWithoutDiffAbsolute()
620620
{
621621
$this->wrapWithTestNow(function () {
622-
$this->assertSame('0 seconds', Chronos::now()->diffForHumans(null, true));
622+
$this->assertSame('just now', Chronos::now()->diffForHumans(null, true));
623623
});
624624
}
625625

0 commit comments

Comments
 (0)