Skip to content

Instantly share code, notes, and snippets.

@rodrigopedra
Created March 25, 2015 14:58
Show Gist options
  • Save rodrigopedra/00ff2941a56b1df5a65d to your computer and use it in GitHub Desktop.
Save rodrigopedra/00ff2941a56b1df5a65d to your computer and use it in GitHub Desktop.
Localize Carbon::diffForHumans in Laravel Models
<?php namespace App;
use App\Carbon\LocalizedDiffForHumansTrait;
use Illuminate\Database\Eloquent\Model;
// example model
class Article extends Model
{
use LocalizedDiffForHumansTrait;
}
<?php namespace App\Carbon;
use Carbon\Carbon;
// extends Carbon\Carbon replacing only the diffForHumans for localized output
class CarbonBR extends Carbon
{
public function diffForHumans( Carbon $other = NULL, $absolute = FALSE )
{
$isNow = $other === NULL;
if ( $isNow )
{
$other = static::now( $this->tz );
}
$diffInterval = $this->diff( $other );
switch ( TRUE )
{
case ( $diffInterval->y > 0 ):
$unit = 'ano';
$delta = $diffInterval->y;
break;
case ( $diffInterval->m > 0 ):
$unit = 'mês';
$delta = $diffInterval->m;
break;
case ( $diffInterval->d > 0 ):
$unit = 'dia';
$delta = $diffInterval->d;
if ( $delta >= self::DAYS_PER_WEEK )
{
$unit = 'semana';
$delta = floor( $delta / self::DAYS_PER_WEEK );
}
break;
case ( $diffInterval->h > 0 ):
$unit = 'hora';
$delta = $diffInterval->h;
break;
case ( $diffInterval->i > 0 ):
$unit = 'minuto';
$delta = $diffInterval->i;
break;
default:
$delta = $diffInterval->s;
$unit = 'segundo';
break;
}
if ( $delta == 0 )
{
$delta = 1;
}
if ($unit === 'mês') {
$txt = $delta == 1 ? $delta . ' mês' : $delta . ' meses';
} else {
$txt = $delta . ' ' . $unit;
$txt .= $delta == 1 ? '' : 's';
}
if ( $absolute )
{
return $txt;
}
$isFuture = $diffInterval->invert === 1;
if ( $isNow )
{
if ( $isFuture )
{
return 'daqui a ' . $txt;
}
return $txt . ' atrás';
}
if ( $isFuture )
{
return $txt . ' depois';
}
return $txt . ' antes';
}
}
<?php namespace App\Carbon;
use DateTime;
// exact same implementation from Illuminate\Database\Eloquent\Model class, changing only Carbon\Carbon with our extended version
trait LocalizedDiffForHumansTrait {
/**
* Return a timestamp as DateTime object.
*
* @param mixed $value
* @return \App\Carbon\CarbonBR
*/
protected function asDateTime($value)
{
// If this value is an integer, we will assume it is a UNIX timestamp's value
// and format a Carbon object from this timestamp. This allows flexibility
// when defining your date fields as they might be UNIX timestamps here.
if (is_numeric($value))
{
return CarbonBR::createFromTimestamp($value);
}
// If the value is in simply year, month, day format, we will instantiate the
// Carbon instances from that format. Again, this provides for simple date
// fields on the database, while still supporting Carbonized conversion.
elseif (preg_match('/^(\d{4})-(\d{2})-(\d{2})$/', $value))
{
return CarbonBR::createFromFormat('Y-m-d', $value)->startOfDay();
}
// Finally, we will just assume this date is in the format used by default on
// the database connection and use that format to create the Carbon object
// that is returned back out to the developers after we convert it here.
elseif ( ! $value instanceof DateTime)
{
$format = $this->getDateFormat();
return CarbonBR::createFromFormat($format, $value);
}
return CarbonBR::instance($value);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment