Skip to content

Commit

Permalink
Use same logic as Carbon for diffForHumans
Browse files Browse the repository at this point in the history
  • Loading branch information
jenssegers committed Mar 12, 2017
1 parent 0b31adf commit 158f708
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 30 deletions.
71 changes: 41 additions & 30 deletions src/Date.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,6 @@ public static function createFromFormat($format, $time, $timezone = null)
*/
public function diffForHumans(Carbon $since = null, $absolute = false, $short = false)
{
// Get translator
$lang = $this->getTranslator();

// Are we comparing against another date?
$relative = ! is_null($since);

Expand All @@ -116,32 +113,43 @@ public function diffForHumans(Carbon $since = null, $absolute = false, $short =
// Are we comparing to a date in the future?
$future = $since->getTimestamp() < $this->getTimestamp();

$units = [
'second' => 60,
'minute' => 60,
'hour' => 24,
'day' => 7,
'week' => 30 / 7,
'month' => 12,
];
// Calculate difference between the 2 dates.
$diff = $this->diff($since);

// Date difference
$difference = abs($since->getTimestamp() - $this->getTimestamp());

// Default unit
$unit = 'year';

// Select the best unit.
foreach ($units as $key => $value) {
if ($difference < $value) {
$unit = $key;
switch (true) {
case $diff->y > 0:
$unit = $short ? 'y' : 'year';
$count = $diff->y;
break;
case $diff->m > 0:
$unit = $short ? 'm' : 'month';
$count = $diff->m;
break;
case $diff->d > 0:
$unit = $short ? 'd' : 'day';
$count = $diff->d;
if ($count >= static::DAYS_PER_WEEK) {
$unit = $short ? 'w' : 'week';
$count = (int) ($count / static::DAYS_PER_WEEK);
}
break;
case $diff->h > 0:
$unit = $short ? 'h' : 'hour';
$count = $diff->h;
break;
case $diff->i > 0:
$unit = $short ? 'min' : 'minute';
$count = $diff->i;
break;
default:
$count = $diff->s;
$unit = $short ? 's' : 'second';
break;
}

$difference = $difference / $value;
}

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

// Select the suffix.
if ($relative) {
Expand All @@ -150,22 +158,25 @@ public function diffForHumans(Carbon $since = null, $absolute = false, $short =
$suffix = $future ? 'from_now' : 'ago';
}

// Get translator instance.
$lang = $this->getTranslator();

// Some languages have different unit translations when used in combination
// with a specific suffix. Here we will check if there is an optional
// translation for that specific suffix and use it if it exists.
if ($lang->trans("${unit}_diff") != "${unit}_diff") {
$ago = $lang->transChoice("${unit}_diff", $difference, [':count' => $difference]);
$ago = $lang->transChoice("${unit}_diff", $count, [':count' => $count]);
} elseif ($lang->trans("${unit}_${suffix}") != "${unit}_${suffix}") {
$ago = $lang->transChoice("${unit}_${suffix}", $difference, [':count' => $difference]);
$ago = $lang->transChoice("${unit}_${suffix}", $count, [':count' => $count]);
} else {
$ago = $lang->transChoice($unit, $difference, [':count' => $difference]);
$ago = $lang->transChoice($unit, $count, [':count' => $count]);
}

if ($absolute) {
return $ago;
}

return $lang->transChoice($suffix, $difference, [':time' => $ago]);
return $lang->transChoice($suffix, $count, [':time' => $ago]);
}

/**
Expand Down Expand Up @@ -238,7 +249,7 @@ public function format($format)

// Short notations.
if (in_array($character, ['D', 'M'])) {
$toTranslate = mb_strtolower($original);
$toTranslate = mb_strtolower($original);
$shortTranslated = $lang->trans($toTranslate);

if ($shortTranslated === $toTranslate) {
Expand Down
17 changes: 17 additions & 0 deletions tests/DateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,23 @@ public function testDiffForHumans()
{
$date = Date::parse('-5 years');
$this->assertSame('5 years ago', $date->diffForHumans());

$date = Date::parse('-15 days');
$this->assertSame('2 weeks ago', $date->diffForHumans());

$date = Date::parse('-13 days');
$this->assertSame('1 week ago', $date->diffForHumans());

$date = Date::parse('-13 days');
$this->assertSame('1 week', $date->diffForHumans(null, true));

$date = Date::parse('-3 months');
$this->assertSame('3 months', $date->diffForHumans(null, true));

$date = Date::parse('-1 week');
$future = Date::parse('+1 week');
$this->assertSame('2 weeks after', $future->diffForHumans($date));
$this->assertSame('2 weeks before', $date->diffForHumans($future));
}

public function testTimespan()
Expand Down

0 comments on commit 158f708

Please sign in to comment.