Skip to content

Instantly share code, notes, and snippets.

@juliancantillo
Forked from IsraelOrtuno/CarbonLocale.php
Created January 29, 2016 21:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save juliancantillo/6506dd9eb4678e753d0b to your computer and use it in GitHub Desktop.
Save juliancantillo/6506dd9eb4678e753d0b to your computer and use it in GitHub Desktop.
Carbon Locale - Use Carbon diffForHuman in different languages with Laravel Lang class
<?php
/**
- * Carbon diffForHumans multi-language.
- * Inspired in a code made by Meroje (https://gist.github.com/Meroje/7783731)
- * This code has been improved as in that original version was only showing
- * past date differences. Not it's able to show differences in past and future.
- *
- * Use it this way:
- *
- * $dt = CarbonLocale::now();
- * echo $dt->diffForHumans($dt->copy()->subMinutes(15));
- *
- * It'll print: 'en 15 minutos' (for Spanish, feel free to translate your own).
- */
use Carbon\Carbon;
use Illuminate\Support\Facades\Lang;
class CarbonLocale extends Carbon {
/**
* Get the difference in a human readable format.
*
* When comparing a value in the past to default now:
* 1 hour ago
* 5 months ago
*
* When comparing a value in the future to default now:
* 1 hour from now
* 5 months from now
*
* When comparing a value in the past to another value:
* 1 hour before
* 5 months before
*
* When comparing a value in the future to another value:
* 1 hour after
* 5 months after
*
* @param Carbon $other
*
* @return string
*/
public function diffForHumans(Carbon $other = null)
{
$isNow = $other === null;
if ($isNow) {
$other = static::now($this->tz);
}
$isFuture = $this->gt($other);
$delta = $other->diffInSeconds($this);
// 4 weeks per month, 365 days per year... good enough!!
$divs = array(
'second' => self::SECONDS_PER_MINUTE,
'minute' => self::MINUTES_PER_HOUR,
'hour' => self::HOURS_PER_DAY,
'day' => self::DAYS_PER_WEEK,
'week' => 4,
'month' => self::MONTHS_PER_YEAR
);
$unit = 'year';
foreach ($divs as $divUnit => $divValue) {
if ($delta < $divValue) {
$unit = $divUnit;
break;
}
$delta = floor($delta / $divValue);
}
if ($delta == 0) {
$delta = 1;
}
// Código adaptado para utilizar el gestor de idiomas de Laravel
$txt = 'carbonlocale';
if ($isFuture) {
return Lang::choice("$txt.past.$unit", $delta, compact('delta'));
}
return Lang::choice("$txt.future.$unit", $delta, compact('delta'));
}
}
<?php
// This file should be located at your lang directory (app/lang/es?)
return array(
'past' => array(
'second' => 'hace :delta segundo|hace :delta segundos',
'minute' => 'hace :delta minuto|hace :delta minutos',
'hour' => 'hace :delta hora|hace :delta horas',
'day' => '{1} ayer|{2} hace dos días|[3,Inf] hace :delta días',
'week' => '{1} hace :delta semana|[2,Inf] hace :delta semanas',
'month' => '{1} hace :delta mes|[2,Inf] hace :delta meses',
'year' => 'hace :delta año|hace :delta años',
),
'future' => array(
'second' => 'en :delta segundo|en :delta segundos',
'minute' => 'en :delta minuto|en :delta minutos',
'hour' => 'en :delta hora|en :delta horas',
'day' => '{1} mañana|{2} en dos días|[3,Inf] en :delta días',
'week' => '{1} en :delta semana|[2,Inf] en :delta semanas',
'month' => '{1} en :delta mes|[2,Inf] en :delta meses',
'year' => 'en :delta año|en :delta años',
)
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment