Carbon: diffForHumans returns 1 second

Created on 3 Mar 2016  路  7Comments  路  Source: briannesbitt/Carbon

Hi, when i do that:

$dtDate1 = Carbon::create(2016, 1, 1, 0 ,0, 0,'Europe/Paris');
$dtDate2 = Carbon::create(2016, 1, 1, 0 ,0, 0,'Europe/Paris');
$diffHumains = $dtDate2->diffForHumans($dtDate1,true);

$diffHumains returns 1 second...

Most helpful comment

I would opt for the facebook wording of "Just now" rather than something as robotic as 0 seconds ago.

All 7 comments

Appears to be on purpose:

if ($count === 0) {
    $count = 1;
}

(https://github.com/briannesbitt/Carbon/blob/master/src/Carbon/Carbon.php#L2399)

The only case in the switch block that can set $count to 0 is the default one. All the other cases skip if the value is not greater than 0. In other words, this will only happen for seconds (the default case).

I am not sure why this would be necessary; "0 seconds ago" is perfectly fine, imo.

I would opt for the facebook wording of "Just now" rather than something as robotic as 0 seconds ago.

+1 form "Just now"

I just into this as well; just now. 馃槣

I would suggest the wording "Same time" when not comparing to now().

It would be a breaking change for everyone that expect to keep having the "1 second" result, and it would mean translate "just now" in 63 languages.

So I recommend to use a custom implementation (extend or macro):

public function customDiffForHumans(self $other = null, $absolute = false, $short = false, $parts = 1)
{
  if ($this->diffInSeconds($other, true) < 1) {
    return "just now"; // You're on you own for translations
  }

  return $this->diffForHumans($other, $absolute, $short, $parts);
}

This issue and #625 could be handled in a common way.

The default behavior must keep being what it's now for backward compatibility.

Settings method or options argument (binary values with pipes) could enable the following features:

  • "Just now" ("now" translation key) if 0 seconds. (Keep 1 second if the "now" translation key is not available in the current locale)
  • "yesterday/tomorrow" translation key if 1 or -1 day. (Keep 1 day after/before if translation keys not available)
  • "avant-hier/apr猫s-demain" ("day_before_yesterday, day_after_tomorrow translation keys), French, Persian and all languages with words for 2 and -2 day (should be translated only if those words are common in those languages)

Will be fixed via #1240

Was this page helpful?
0 / 5 - 0 ratings