PHP: multilingual Time::since() static class I didn't make the math myself since I'm an idiot. I did however try to make it more versatile and usable for my multilingual page, which involves making it simple to use for advanced plural forms such as Polish and Russian.

Is this code any good to use on a website? or is it overkill?
I'm using the laravel framework, but this code should work everywhere.

I use it like this :

Time::since($user-&gt;updated_at)


And here is the code:

class Time
{
public static function since($timestamp) {
    date_default_timezone_set('Europe/Rome');
    $time = strtotime($timestamp);
    $time = time() - $time;

    $tokens = array (
    31536000 =&gt; 'year',
    2592000 =&gt; 'month',
    604800 =&gt; 'week',
    86400 =&gt; 'day',
    3600 =&gt; 'hour',
    60 =&gt; 'minute',
    1 =&gt; 'second'
    );

    foreach ($tokens as $unit =&gt; $text) {
        if ($time &lt; $unit) continue;
        $numberOfUnits = floor($time / $unit);
        switch ($text) 
        {
        case 'year':
        return sprintf(ngettext(
            "Last visit: %d year ago", 
            "Last visit: %d years ago", 
            $numberOfUnits), $numberOfUnits);
            break;
        case 'month':
            return sprintf(ngettext(
            "Last visit: %d month ago", 
            "Last visit: %d months ago", 
            $numberOfUnits), $numberOfUnits);
            break;
        case 'week':
            return sprintf(ngettext(
            "Last visit: %d week ago",
            "Last visit: %d weeks ago",
            $numberOfUnits), $numberOfUnits);
            break;
        case 'day':
            return sprintf(ngettext(
            "Last visit: %d day ago",
            "Last visit: %d days ago",
            $numberOfUnits), $numberOfUnits);
            break;
        case 'hour':
            return sprintf(ngettext(
            "Last visit: %d hour ago",
            "Last visit: %d hours ago",
            $numberOfUnits), $numberOfUnits);
            break;
            case 'minute':
            return sprintf(ngettext(
            "Last visit: %d minute ago",
            "Last visit: %d minutes ago",
            $numberOfUnits), $numberOfUnits);
            break;
            case 'second':
            return sprintf(ngettext(
            "Last visit: %d second ago",
            "Last visit: %d seconds ago",
            $numberOfUnits), $numberOfUnits);
            break;
            }
        }
    }
}
I've not coded any PHP in last five years but if I'm right the following is the same:

$tokens = array (
    31536000 =&gt; array('msgid1' =&gt; "Last visit: %d year ago", 
        'msgid2' =&gt; "Last visit: %d years ago"),
    2592000 =&gt; array('msgid1' =&gt; "Last visit: %d month ago", 
        'msgid2' =&gt; "Last visit: %d months ago"),
    604800 =&gt; array('msgid1' =&gt; "Last visit: %d week ago", 
        'msgid2' =&gt; "Last visit: %d weeks ago"),
    86400 =&gt; array('msgid1' =&gt; "Last visit: %d day ago", 
        'msgid2' =&gt; "Last visit: %d days ago"),
    3600 =&gt; array('msgid1' =&gt; "Last visit: %d hour ago", 
        'msgid2' =&gt; "Last visit: %d hours ago"),
    60 =&gt; array('msgid1' =&gt; "Last visit: %d minute ago", 
        'msgid2' =&gt; "Last visit: %d minutes ago"),
    1 =&gt; array('msgid1' =&gt; "Last visit: %d second ago", 
        'msgid2' =&gt; "Last visit: %d seconds ago")
);

foreach ($tokens as $unit =&gt; $data) {
    if ($time &lt; $unit) {
        continue;
    }
    $numberOfUnits = floor($time / $unit);

    $formatString = ngettext($data['msgid1'], $data['msgid2'], $numberOfUnits);
    return sprintf($formatString, $numberOfUnits);
}


I think you should handle 0 too. Currently it prints nothing for this input.