Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

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->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 => 'year',
    2592000 => 'month',
    604800 => 'week',
    86400 => 'day',
    3600 => 'hour',
    60 => 'minute',
    1 => 'second'
    );

    foreach ($tokens as $unit => $text) {
        if ($time < $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;
            }
        }
    }
} 
share|improve this question
add comment

1 Answer 1

I've not coded any PHP in last five years but if I'm right the following is the same:

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

foreach ($tokens as $unit => $data) {
    if ($time < $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.

share|improve this answer
1  
Great idea, and it works. You just have to wrap the strings in _() and translate them in the .po file. Much better. –  rococo polkadot bandit Nov 22 '12 at 12:00
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.