Another variation on the "time ago" PHP function, use MySQL's datetime field type

Comments (5)

I was recently looking at writing a function to format the comment post times in the ever trendy "time ago" style... such as;


Blah blah posted 6 days 3 hours ago

Naturally I first scoured the web for an appropriate existing PHP function, and I came across this one; Digg Like Time Ago PHP Function.

Quite useful and fairly complete, but there were a few things I wanted to change, so I've hacked it up and remixed it here to accept a MySQL datetime field type result and then format the output like mentioned above. I also added weeks, months, years, and decades to the function to carry it as far out as anyone would possibly want it to go.

If you're using sNews and want to use this for your comments, simply follow these 2 quick steps.

Step 1) As always, BACK-UP your snews.php file and work off a copy. Now simply copy the function below into your snews.php file, right above the closing php tag;

// DISPLAYS COMMENT POST TIME AS "1 year, 1 week ago" or "5 minutes, 7 seconds ago", etc...
function time_ago($date,$granularity=2) {
    $date = strtotime($date);
    $difference = time() - $date;
    $periods = array('decade' => 315360000,
        'year' => 31536000,
        'month' => 2628000,
        'week' => 604800, 
        'day' => 86400,
        'hour' => 3600,
        'minute' => 60,
        'second' => 1);
                                 
    foreach ($periods as $key => $value) {
        if ($difference >= $value) {
            $time = floor($difference/$value);
            $difference %= $value;
            $retval .= ($retval ? ' ' : '').$time.' ';
            $retval .= (($time > 1) ? $key.'s' : $key);
            $granularity--;
        }
        if ($granularity == '0') { break; }
    }
    return ' posted '.$retval.' ago';      
}

Step 2) Now find the function comment, and comment out the existing date format and add the highlighted code like below;

while ($r = mysql_fetch_array($result)) {
    //$date = date($date_format, strtotime($r['time']));
    $date = time_ago($r['time']);
    commentNum = $offset + $ordinal;

That should do it, now your comment times should be formatted by "time ago". Yes, you may notice that I'm not using using this here on my main site, and you are correct, I'm not :).. right now that is, but I may in the future. I just thought it may be useful to someone else looking for the functionality.

bookmark / share this: Bookmark and Share
rated 4/5 (9 votes)


5 comments

Add a new comment »

slemborg slemborg said:
Jun 2nd, 2009 at 5:39 pm

Looking really good, i'm gonna try it out tomorrow, good job.


Pico RG Pico RG said:
Nov 21st, 2009 at 7:48 pm

Thanks for the script, it seems very interesting


Michael Michael said:
Dec 28th, 2009 at 9:01 am

Thanks for the function, but it has an error in it. The 'round' should be 'floor' or 'intval'. As an example, if you pass it a date that is between 23.5 and 24 hours in the past, it will return '24 hours 30+x minutes ago', but it should return '23 hours 30+x minutes ago'.


Matt Matt said:
Dec 28th, 2009 at 9:50 am

Good catch Michael, it should use floor. I will change that. Thanks!


Eric Eric said:
Jun 19th, 2010 at 6:04 pm

Thanks Matt, I was looking for just this thing, and it works like a champ. Saved me a bunch of time, I'm sure.



Write a comment

* = required field

:

:

:

:

You may insert urls in plain text, urls will be automatically linkified for trusted users and on seasoned posts only. All first comments are moderated, so use your email if you want to be remembered.


Back to top