CoHoop / CoHoop-Rails

CoHoop's Rails application
2 stars 0 forks source link

[Microhoops] Insert date #16

Closed gabriel-dehan closed 11 years ago

gabriel-dehan commented 11 years ago

Microhoops should display a date when showed in the form xx seconds/minutes/hours ago and in the form "On the XX of XX" if they are older

Yoksy commented 11 years ago

If you want, I've got an old PHP function from cifamily, not sure if its optimized (and its in french yes) :

$current_date = new DateTime('now');
$previous_date = new DateTime($date);
$interval = $previous_date->diff($current_date);
$translated_date = translateDate($previous_date->format('l j F'));
$s = '';

if ($interval->y > 1) {
    //Jeudi 03 mars 2010
    return $date_posted = 'le ' . $translated_date . ' ' . $previous_date->format('Y');
}
elseif ($interval->d > 2) {
    //Jeudi 03 mars
    return $date_posted = 'le ' . $translated_date;
}
elseif ($interval->d >= 1) {
    //Hier à 02:21
    return $date_posted = 'hier à ' . $previous_date->format('h:i');
}
elseif ($interval->h >= 1) {
    //Il y a 7 heure(s)
    if ($interval->h > 1) $s = 's';
    return $date_posted = 'il y a ' . $interval->h .  ' heure' . $s;
}
elseif ($interval->h < 1 && $interval->i >= 1) {
    //Il y a 4 minute(s)
    if ($interval->i > 1) $s = 's';
    return $date_posted = 'il y a ' . $interval->i .  ' minute' . $s;
}
else {
    //Il y a 12 seconde(s)
    if ($interval->s > 1) $s = 's';
    return $date_posted = 'il y a ' . $interval->s .  ' seconde' . $s;
}