Skip to content

Commit

Permalink
Use Carbon diffForHumans
Browse files Browse the repository at this point in the history
  • Loading branch information
jenssegers committed Mar 1, 2018
1 parent 88c4b27 commit 7695f34
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 96 deletions.
81 changes: 0 additions & 81 deletions src/Date.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,87 +98,6 @@ public static function createFromFormat($format, $time, $timezone = null)
return parent::createFromFormat($format, $time, $timezone);
}

/**
* @inheritdoc
*/
public function diffForHumans(Carbon $since = null, $absolute = false, $short = false)
{
// Are we comparing against another date?
$relative = ! is_null($since);

if (is_null($since)) {
$since = new static('now', $this->getTimezone());
}

// Are we comparing to a date in the future?
$future = $since->getTimestamp() < $this->getTimestamp();

// Calculate difference between the 2 dates.
$diff = $this->diff($since);

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;
}

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

// Select the suffix.
if ($relative) {
$suffix = $future ? 'after' : 'before';
} else {
$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", $count, [':count' => $count]);
} elseif ($lang->trans("${unit}_${suffix}") != "${unit}_${suffix}") {
$ago = $lang->transChoice("${unit}_${suffix}", $count, [':count' => $count]);
} else {
$ago = $lang->transChoice($unit, $count, [':count' => $count]);
}

if ($absolute) {
return $ago;
}

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

/**
* Alias for diffForHumans.
*
Expand Down
15 changes: 8 additions & 7 deletions src/Lang/de.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,13 @@
'sat' => 'Sa',
'sun' => 'So',

'year_diff' => '1 Jahr|:count Jahren',
'month_diff' => '1 Monat|:count Monaten',
'week_diff' => '1 Woche|:count Wochen',
'day_diff' => '1 Tag|:count Tagen',
'hour_diff' => '1 Stunde|:count Stunden',
'minute_diff' => '1 Minute|:count Minuten',
'second_diff' => '1 Sekunde|:count Sekunden',
'year_from_now' => '1 Jahr|:count Jahren',
'month_from_now' => '1 Monat|:count Monaten',
'week_from_now' => '1 Woche|:count Wochen',
'day_from_now' => '1 Tag|:count Tagen',
'year_ago' => '1 Jahr|:count Jahren',
'month_ago' => '1 Monat|:count Monaten',
'week_ago' => '1 Woche|:count Wochen',
'day_ago' => '1 Tag|:count Tagen',

];
16 changes: 8 additions & 8 deletions src/Lang/ta.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
*/

'ago' => ':time முன்பு',
'from_now' => ':time|:time',
'from_now' => ':time',
'after' => ':time கழித்து',
'before' => ':time முன்னால்',
'year' => '1 ஆண்டு|:count ஆண்டுகள்',
Expand Down Expand Up @@ -58,12 +58,12 @@
'month_ago' => '1 மாதம்|:count மாதங்களுக்கு',
'year_ago' => '1 ஆண்டு|:count ஆண்டுகளுக்கு',

'second_from_now' => ':count வினாடியில்|:count வினாடிகளில்',
'minute_from_now' => ':count நிமிடத்தில்|:count நிமிடங்களில்',
'hour_from_now' => ':count மணி நேரத்தில்|:count மணி நேரத்தில்',
'day_from_now' => ':count நாளில்|:count நாட்களில்',
'week_from_now' => ':count வாரத்தில்|:count வாரங்களில்',
'month_from_now' => ':count மாதத்தில்|:count மாதங்களில்',
'year_from_now' => ':count ஆண்டில்|:count ஆண்டுகளில்'
'second_from_now' => '1 வினாடியில்|:count வினாடிகளில்',
'minute_from_now' => '1 நிமிடத்தில்|:count நிமிடங்களில்',
'hour_from_now' => '1 மணி நேரத்தில்|:count மணி நேரத்தில்',
'day_from_now' => '1 நாளில்|:count நாட்களில்',
'week_from_now' => '1 வாரத்தில்|:count வாரங்களில்',
'month_from_now' => '1 மாதத்தில்|:count மாதங்களில்',
'year_from_now' => '1 ஆண்டில்|:count ஆண்டுகளில்'

];

0 comments on commit 7695f34

Please sign in to comment.