Main
Index
Search
Posts
Who's
Online
Log
In

Home: Products: Off Topic / Other:
Facebook-style relative times (e.g. "7 hours ago")

 

 


Chris
Staff


Jan 6, 2011, 5:02 PM

Post #1 of 7 (35846 views)
Shortcut
Facebook-style relative times (e.g. "7 hours ago") Can't Post

Hi everyone,

I just wrote a bit of code that I thought someone might find useful; it turns a date into a more human-readable format such as "37 seconds ago", "about an hour ago", "Wednesday at 6:07pm", or "February 17, 2007", depending on how far in the past the date is. It doesn't handle future-dates at all.


Code
<?php 
function pretty_relative_time($time) {
if ($time !== intval($time)) { $time = strtotime($time); }
$d = time() - $time;
if ($time < strtotime(date('Y-m-d 00:00:00')) - 60*60*24*3) {
$format = 'F j';
if (date('Y') !== date('Y', $time)) {
$format .= ", Y";
}
return date($format, $time);
}
if ($d >= 60*60*24) {
$day = 'Yesterday';
if (date('l', time() - 60*60*24) !== date('l', $time)) { $day = date('l', $time); }
return $day . " at " . date('g:ia', $time);
}
if ($d >= 60*60*2) { return intval($d / (60*60)) . " hours ago"; }
if ($d >= 60*60) { return "about an hour ago"; }
if ($d >= 60*2) { return intval($d / 60) . " minutes ago"; }
if ($d >= 60) { return "about a minute ago"; }
if ($d >= 2) { return intval($d) . " seconds ago"; }
return "a few seconds ago";
}
?>

USAGE:
<?php echo pretty_relative_time($record['createdDate']); ?>


I hope this is helpful to someone! :D
Chris


(This post was edited by Damon on Feb 7, 2011, 10:56 AM)


Rusty
User

Jan 6, 2011, 5:16 PM

Post #2 of 7 (35842 views)
Shortcut
Re: [chris] Facebook-style relative times (e.g. "7 hours ago") [In reply to] Can't Post

Ack!! Thanks, that's awesome! Very useful for Blog posts.

I love IF statements!

*saves*
Rusty


(This post was edited by Rusty on Jan 6, 2011, 5:17 PM)


Toledoh
Enthusiast

Jan 7, 2011, 11:22 PM

Post #3 of 7 (35815 views)
Shortcut
Re: [chris] Facebook-style relative times (e.g. "7 hours ago") [In reply to] Can't Post

Thanks Chris!

Please, more little tips and techniques like this would be great!
Cheers,

Tim Forrest
Toledoh Enterprises
www.toledoh.com.au


Toledoh
Enthusiast

Feb 6, 2011, 8:21 PM

Post #4 of 7 (32305 views)
Shortcut
Re: [Chris] Facebook-style relative times (e.g. "7 hours ago") [In reply to] Can't Post

I think it's meant to be

Code
USAGE:  
<?php echo pretty_relative_time($record['createdDate']); ?>


"pretty_relative_time" not "pretty_relative_date"
Cheers,

Tim Forrest
Toledoh Enterprises
www.toledoh.com.au

(This post was edited by Toledoh on Feb 6, 2011, 8:21 PM)


Damon
Staff / Moderator


Feb 7, 2011, 10:58 AM

Post #5 of 7 (32052 views)
Shortcut
Re: [Toledoh] Facebook-style relative times (e.g. "7 hours ago") [In reply to] Can't Post

Hi Tim,

Thanks for noting and pointing that out.

I have updated Chris's code example above with the correct function name.

--------------------------------------------------- 
Cheers
Damon Edis
interactivetools.com

Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/


jhon11
New User

Jun 14, 2011, 3:06 AM

Post #6 of 7 (26895 views)
Shortcut
Re: [Chris] Facebook-style relative times (e.g. "7 hours ago") [In reply to] Can't Post

This is awesome dude.Can you post more useful code.This is really cool.


anndria4ever
New User

Oct 16, 2011, 8:01 AM

Post #7 of 7 (18742 views)
Shortcut
Re: [Chris] Facebook-style relative times (e.g. "7 hours ago") [In reply to] Can't Post

Smile Thanks for the added info. Very helpful indeed. Gonna look forward for another tips. Tongue