Created
April 22, 2012 19:57
-
-
Save jak/2466500 to your computer and use it in GitHub Desktop.
Revisions
-
Jak Spalding created this gist
Apr 22, 2012 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,39 @@ <?php function pluralise($count, $single, $plural = null) { if ($count == 1 || $count == -1) { return $single; } if ($plural == null) { $plural = $single . 's'; } return $plural; } function from_time($time) { $now = new DateTime('now'); $then = new DateTime($time); $timespan = $now->diff($then); $message = array(); if ($timespan->y) $message[] = $timespan->y.' '.pluralise($timespan->y, 'year'); if ($timespan->m) $message[] = $timespan->m.' '.pluralise($timespan->m, 'month'); if ($timespan->d) $message[] = $timespan->d.' '.pluralise($timespan->d, 'day'); if ($timespan->h) $message[] = $timespan->h.' '.pluralise($timespan->h, 'hour'); if ($timespan->i) $message[] = $timespan->i.' '.pluralise($timespan->i, 'minute'); if ($timespan->s) $message[] = $timespan->s.' '.pluralise($timespan->s, 'second'); if (count($message) == 0) { return 'just now'; } $output = array_shift($message); if ($message) { $output .= ' and '.array_shift($message); } $output .= ' ago'; return $output; }