Permalink
Browse files

Change human_time() to support durations in the future, and to allow …

…omission of the "ago"/"from now" suffix
  • Loading branch information...
1 parent ffff19a commit 0298673de9e40e1484baa0d20f33b11e5f9149d1 @cdhowie committed Jul 7, 2011
Showing with 15 additions and 2 deletions.
  1. +15 −2 htdocs/common.inc.php
View
@@ -192,8 +192,15 @@ function get_tempdata($key)
$HUMAN_TIME_POSTFIX = array("second", "minute", "hour", "day", "week", "month", "year");
$HUMAN_TIME_LENGTHS = array(60, 60, 24, 7, 4.3452380952380952380952380952381, 12);
-function human_time($difference)
+function human_time($difference, $suffix = true)
{
+ $future = false;
+
+ if ($difference < 0) {
+ $future = true;
+ $difference = -$difference;
+ }
+
global $HUMAN_TIME_POSTFIX, $HUMAN_TIME_LENGTHS;
for ($i = 0; $difference >= $HUMAN_TIME_LENGTHS[$i]; $i++)
@@ -206,7 +213,13 @@ function human_time($difference)
if ($difference != 1)
$postfix .= "s";
- return "$difference $postfix ago";
+ $str = "$difference $postfix";
+
+ if ($suffix) {
+ $str .= ' ' . ($future ? 'from now' : 'ago');
+ }
+
+ return $str;
}
function format_date($date)

0 comments on commit 0298673

Please sign in to comment.