Another variation on the "time ago" PHP function, use MySQL's datetime field type
filed under: Web Development / PHP Programming
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.
5 comments
Add a new comment »Categories
Recent Entries
Recent Comments
- Dede (I checked it today in a shop. GT2 had some troubles with six ...)
- Matt (Bintang, You need to re-direct the url, try ...)
- jesth (Ohh.. why didn't I think of that, thanks alot.)
- Matt (Dede, I don't have Gran Turismo 2, any of the 2nd+ generation ...)
- Matt (Jesth, Just change the if condition, instead of looking for ...)
- jesth (Hi (again) Was wondering, is it possible to make it ...)
- Bintang Sembilan (Matt, thanks for your modd. I have apply it to my ...)
- Dede (Hello there. Can you check something for me? I want to buy ...)
- Matt (I think it's a driver issue Terrence, or it was a driver issue. ...)
Popular Entries
- Light-weight related articles mod for sNews 1.7 (4.5/5)
- Image / math hybrid captcha version 2, vastly improved (4.42/5)
- 1024x600 netbook wallpapers of Evangeline Lilly (4.4/5)
- Compact archives for sNews 1.7 (4.4/5)
- sNews Ajax Polls mod now available (4.38/5)
- Pretty date and comments bars in sNews CMS (4.35/5)
- Page caching mod for sNews 1.7 (4.33/5)
- Gravatar mod for sNews 1.7 (4.29/5)
- An improved tag cloud for sNews 1.7 (4.29/5)
Jun 2nd, 2009 at 5:39 pm
Looking really good, i'm gonna try it out tomorrow, good job.
Nov 21st, 2009 at 7:48 pm
Thanks for the script, it seems very interesting
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'.
Dec 28th, 2009 at 9:50 am
Good catch Michael, it should use floor. I will change that. Thanks!
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.