Skip to content

Instantly share code, notes, and snippets.

@Meroje
Created December 4, 2013 07:43
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Meroje/7783731 to your computer and use it in GitHub Desktop.
Save Meroje/7783731 to your computer and use it in GitHub Desktop.
TimeDiff For Humans
<?php
use Carbon\Carbon;
class Comment extends Eloquent {
protected $guarded = array();
protected $softDelete = true;
public static $rules = array();
public static function publish($token)
{
$comment = self::withTrashed()->where('token', $token)->first();
if($comment)
{
$comment->restore();
$comment->published_at = new Carbon();
return $comment->save();
} else {
return false;
}
}
public function scopePublished($query)
{
return $query->whereNotNull('published_at');
}
public function getDates()
{
return array('created_at', 'updated_at', 'deleted_at', 'published_at');
}
public function getPublishedAttribute()
{
$txt = 'app.timediff.';
$isNow = true;
$other = Carbon::now();
$delta = abs($other->diffInSeconds($this->created_at));
// 30 days per month, 365 days per year... good enough!!
$divs = array(
'second' => Carbon::SECONDS_PER_MINUTE,
'minute' => Carbon::MINUTES_PER_HOUR,
'hour' => Carbon::HOURS_PER_DAY,
'day' => 30,
'month' => Carbon::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;
}
$txt .= $unit;
return Lang::choice($txt, $delta, compact('delta'));
}
}
<?php
return array(
'timediff' => array(
'second' => 'Es gibt :delta Sekunde|Es gibt :delta Sekundes',
'minute' => 'Es gibt :delta Minute|Es gibt :delta Minutes',
'hour' => 'Vor :delta Stunde|Vor :delta Stundes',
'day' => '{1} Gestern|{2} Vorgestern|[3,Inf] Vor :delta Tagen',
'month' => '[1,Inf] Vor :delta Monat',
'year' => 'Vor :delta Jahr|Es geben :delta an Jahr',
),
);
<?php
return array(
'timediff' => array(
'second' => ':delta second ago|:delta seconds ago',
'minute' => ':delta minute ago|:delta minutes ago',
'hour' => ':delta hour ago|:delta hours ago',
'day' => '{1} Yesterday|{2} Two days ago|[3,Inf] :delta days ago',
'month' => '{1} :delta month ago|[2,Inf] :delta month ago',
'year' => ':delta year ago|:delta years ago',
),
);
<?php
return array(
'timediff' => array(
'second' => 'Il y a :delta seconde|Il y a :delta secondes',
'minute' => 'Il y a :delta minute|Il y a :delta minutes',
'hour' => 'Il y a :delta heure|Il y a :delta heures',
'day' => '{1} Hier|{2} Avant-hier|[3,Inf] Il y a :delta jours',
'month' => '[1,Inf] Il y a :delta mois',
'year' => 'Il y a :delta an|Il y a :delta ans',
),
);
@jartaud
Copy link

jartaud commented Aug 6, 2014

es.php

<?php
     return array(
        'timediff' => 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} Antes de ayer|[3,Inf] Hace :delta días',
             'month' => 'Hace :delta mes|Hace :delta meses',
             'year' => 'Hace :delta año|Hace :delta años',
       ),
);

@jaripekkala
Copy link

fi.php

    'second' => ':delta sekunti sitten|:delta sekuntia sitten',
    'minute' => ':delta minuutti sitten|:delta minuuttia sitten',
    'hour'   => ':delta tunti sitten|:delta tuntia sitten',
    'day'    => '{1} Eilen|{2} Toissa päivänä|[3,Inf] :delta päivää sitten',
    'month'  => '{1} Kuukausi sitten|[2,Inf] :delta kuukautta sitten',
    'year'   => 'Vuosi sitten|:delta vuotta sitten',

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment