From c19ac9fbae2e6e4d20487c12c4cb677b3150eb37 Mon Sep 17 00:00:00 2001 From: Josh Sherman Date: Sat, 29 Dec 2012 11:41:23 -0500 Subject: [PATCH] Added relative time function Time::ago($time) will return something like "45 minutes ago" or "45 minutes from now". As always, sticking to GMT/UTC --- classes/Time.php | 80 ++++++++++++++++++++++++++++++++++++++++++++++++ jar.php | 80 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 160 insertions(+) diff --git a/classes/Time.php b/classes/Time.php index 2542f1b..758c976 100644 --- a/classes/Time.php +++ b/classes/Time.php @@ -116,6 +116,7 @@ class Time * @static * @param string $date birth / inception date * @return integer $age number of years old + * @todo Wondering if this really should live in the Date class since it's a Date function. Could flip the aliasing to preserve any older code. */ public static function age($date) { @@ -136,6 +137,85 @@ class Time return $age; } + /** + * Ago + * + * Generates a relative time (e.g. X minutes ago). + * + * @static + * @param mixed $time timestamp to calculate from + * @return string relative time + */ + public static function ago($time) + { + $current = strtotime(Time::timestamp()); + $time = preg_match('/^\d+$/', $time) ? $time : strtotime($time); + + if ($current == $time) + { + $time_ago = 'just now'; + } + else + { + if ($current > $time) + { + $difference = $current - $time; + $suffix = ' ago'; + } + else + { + $difference = $time - $current; + $suffix = ' from now'; + } + + // Less than 1 minute ago (seconds ago) + if ($difference < 60) + { + $time_ago = 'seconds'; + } + // Less than 1 hour ago (minutes ago) + elseif ($difference < 3600) + { + $minutes = round($difference / 60); + $time_ago = $minutes . ' minute' . ($minutes != 1 ? 's' : ''); + } + // Less than 1 day ago (hours ago) + elseif ($difference < 86400) + { + $hours = round($difference / 3600); + $time_ago = $hours . ' hour' . ($hours != 1 ? 's' : ''); + } + // Less than 1 week ago (days ago) + elseif ($difference < 604800) + { + $days = round($difference / 86400); + $time_ago = $days . ' day' . ($days != 1 ? 's' : ''); + } + // Less than 1 month ago (weeks ago) + elseif ($difference < 2419200) + { + $weeks = round($difference / 604800); + $time_ago = $weeks . ' week' . ($weeks != 1 ? 's' : ''); + } + // Less than 1 year ago (months ago) + elseif ($difference < 31449600) + { + $months = round($difference / 2419200); + $time_ago = $months . ' month' . ($months != 1 ? 's' : ''); + } + // Over 1 year ago (years ago) + else + { + $years = round($difference / 31449600); + $time_ago = $years . ' year' . ($years != 1 ? 's' : ''); + } + + $time_ago .= $suffix; + } + + return $time_ago; + } + /** * Timestamp * diff --git a/jar.php b/jar.php index 5699bcc..e29fd92 100755 --- a/jar.php +++ b/jar.php @@ -7746,6 +7746,7 @@ class Time * @static * @param string $date birth / inception date * @return integer $age number of years old + * @todo Wondering if this really should live in the Date class since it's a Date function. Could flip the aliasing to preserve any older code. */ public static function age($date) { @@ -7766,6 +7767,85 @@ class Time return $age; } + /** + * Ago + * + * Generates a relative time (e.g. X minutes ago). + * + * @static + * @param mixed $time timestamp to calculate from + * @return string relative time + */ + public static function ago($time) + { + $current = strtotime(Time::timestamp()); + $time = preg_match('/^\d+$/', $time) ? $time : strtotime($time); + + if ($current == $time) + { + $time_ago = 'just now'; + } + else + { + if ($current > $time) + { + $difference = $current - $time; + $suffix = ' ago'; + } + else + { + $difference = $time - $current; + $suffix = ' from now'; + } + + // Less than 1 minute ago (seconds ago) + if ($difference < 60) + { + $time_ago = 'seconds'; + } + // Less than 1 hour ago (minutes ago) + elseif ($difference < 3600) + { + $minutes = round($difference / 60); + $time_ago = $minutes . ' minute' . ($minutes != 1 ? 's' : ''); + } + // Less than 1 day ago (hours ago) + elseif ($difference < 86400) + { + $hours = round($difference / 3600); + $time_ago = $hours . ' hour' . ($hours != 1 ? 's' : ''); + } + // Less than 1 week ago (days ago) + elseif ($difference < 604800) + { + $days = round($difference / 86400); + $time_ago = $days . ' day' . ($days != 1 ? 's' : ''); + } + // Less than 1 month ago (weeks ago) + elseif ($difference < 2419200) + { + $weeks = round($difference / 604800); + $time_ago = $weeks . ' week' . ($weeks != 1 ? 's' : ''); + } + // Less than 1 year ago (months ago) + elseif ($difference < 31449600) + { + $months = round($difference / 2419200); + $time_ago = $months . ' month' . ($months != 1 ? 's' : ''); + } + // Over 1 year ago (years ago) + else + { + $years = round($difference / 31449600); + $time_ago = $years . ' year' . ($years != 1 ? 's' : ''); + } + + $time_ago .= $suffix; + } + + return $time_ago; + } + /** * Timestamp *