Created
January 22, 2013 22:47
-
-
Save ericrallen/4599412 to your computer and use it in GitHub Desktop.
Revisions
-
Eric Allen created this gist
Jan 22, 2013 .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,107 @@ <?php /*-------------------------------------------------------------------------- This function accepts any date string that str_to_time() can handle and returns a relative date string It can account for seconds, minutes, days, weeks, months, years, decades, centuries, and millenia It does not currently support dates in the future This function was heavily modified from a StackOverflow answer --------------------------------------------------------------------------*/ //BEGIN borrowed: http:stackoverflow.com/questions/11/calculating-relative-time/12#answer-18393 function time_since($original = null) { //make sure we were given a time if($original) { //make sure date is a valid unix timestamp //http:stackoverflow.com/questions/2524680/check-whether-the-string-is-a-unix-timestamp#answer-2524761 if((string) (int) $original !== $original) { //go ahead and convert it, just to be sure $original = date('U', strtotime($original)); } //time periods $second = 1; //1 second in Unix Time $minute = $second * 60; //1 minut in Unix Time $hour = $minute * 60; //1 hour in Unix Time $day = $hour * 24; //1 day in Unix Time $week = $day * 7; //1 week in Unix Time $month = $day * 30; //1 month in Unix Time $year = $day * 365; //1 year in Unix Time $decade = $year * 10; //10 years in Unix Time $century = $decade * 10; //100 years in Unix Time $millenium = $century * 10; //1,000 years in Unix Time $today = time(); //current Unix Time //initialize vars to store our time and name $name = ''; $total = 0; //get seconds since the date given to the function $since = $today - $original; //create array to store textual representation and Unix second representations $checks = array( array('second', $second), array('minute', $minute), array('hour', $hour), array('day', $day), array('week', $week), array('month', $month), array('year', $year), array('decade', $decade), array('century', $century), array('millenium', $millenium) ); //iterate through checks to find the largest value for relative time display foreach($checks as $key => $check) { $seconds = $check[1]; $count = floor($since / $seconds); //if we can't go any further, exit loop if($count < 1) { break; //if we can, update the name and the total } else { $name = $check[0]; $total = $count; } } //begin creating our return value by adding total $return = $total; //if it's greater than 1 if($total > 1) { //if this is millenia, change the pluralization accordingly if($name == 'millenium') { $return .= ' ' . 'millenia'; //if this is centuries, change the pluralization accordingly } elseif($name == 'century') { $return .= ' ' . 'centuries'; //otherwise just add an s } else { $return .= ' ' . $name . 's'; } //if it isn't, just return the singular name } else { $return .= ' ' . $name; } //append 'ago' to the return string $return .= ' ago'; //return time since return $return; } else { return $false; } } //END borrowed ?>