From 7c230224f93c0557d9336609de15b28a5a7bcdb0 Mon Sep 17 00:00:00 2001 From: Sven Slootweg Date: Fri, 29 Jun 2012 06:47:00 +0200 Subject: [PATCH] Add time_ago function for generating a textual representation of time difference. --- include.datetime.php | 98 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 98 insertions(+) diff --git a/include.datetime.php b/include.datetime.php index 9d2eca7..24bc875 100644 --- a/include.datetime.php +++ b/include.datetime.php @@ -178,3 +178,101 @@ function floor_to_day($timestamp) { return floor($timestamp / (60 * 60 * 24)) * (60 * 60 * 24); } + +function time_ago($timestamp, $locale) +{ + if(is_numeric($timestamp)) + { + if($timestamp > time()) + { + $sTimeAgo = $locale->strings['event-future']; + } + elseif($timestamp == time()) + { + $sTimeAgo = $locale->strings['event-now']; + } + else + { + $date1 = new DateTime("@{$timestamp}", new DateTimeZone("GMT")); + $date2 = new DateTime("now", new DateTimeZone("GMT")); + + $interval = $date1->diff($date2); + $years = (int)$interval->format("%G"); + $months = (int)$interval->format("%m"); + $weeks = (int)$interval->format("%U"); + $days = (int)$interval->format("%d"); + $hours = (int)$interval->format("%H"); + $minutes = (int)$interval->format("%i"); + $seconds = (int)$interval->format("%S"); + + if($years > 1) + { + $sTimeAgo = sprintf($locale->strings['event-years-ago'], $years); + } + elseif($years == 1) + { + $sTimeAgo = $locale->strings['event-1year-ago']; + } + elseif($months > 1) + { + $sTimeAgo = sprintf($locale->strings['event-months-ago'], $months); + } + elseif($months == 1) + { + $sTimeAgo = $locale->strings['event-1month-ago']; + } + elseif($weeks > 1) + { + $sTimeAgo = sprintf($locale->strings['event-weeks-ago'], $weeks); + } + elseif($weeks == 1) + { + $sTimeAgo = $locale->strings['event-1week-ago']; + } + elseif($days > 1) + { + $sTimeAgo = sprintf($locale->strings['event-days-ago'], $days); + } + elseif($days == 1) + { + $sTimeAgo = $locale->strings['event-1day-ago']; + } + elseif($hours > 1) + { + $sTimeAgo = sprintf($locale->strings['event-hours-ago'], $hours); + } + elseif($hours == 1) + { + $sTimeAgo = $locale->strings['event-1hour-ago']; + } + elseif($minutes > 1) + { + $sTimeAgo = sprintf($locale->strings['event-minutes-ago'], $minutes); + } + elseif($minutes == 1) + { + $sTimeAgo = $locale->strings['event-1minute-ago']; + } + elseif($seconds > 1) + { + $sTimeAgo = sprintf($locale->strings['event-seconds-ago'], $seconds); + } + elseif($seconds == 1) + { + $sTimeAgo = $locale->strings['event-1second-ago']; + } + else + { + /* If you see this, there's probably something wrong. */ + $sTimeAgo = $locale->strings['event-past']; + } + + } + + return $sTimeAgo; + } + else + { + throw new Exception("The given timestamp is not numeric. Timestamps must be provided in Unix timestamp format."); + } +}