RELATIVE DATES - FACEBOOK STYLE - May 8th, 2011
|
Chris Waddell from Interactive Tools just wrote a function that 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.)
This code can go anywhere before it's first used:
<?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"; } ?>
And here's how to call it in the body of your page:
<?php echo pretty_relative_date($record['createdDate']); ?>
|
The materials on this web site have been created for use with CMS Builder content management software. CMS Builder software is published and licensed for use by InteractiveTools.com. Please contact
Interactive Tools for information on the downloading of the software or the purchasing of licenses.