Return to Snippet

Revision: 61614
at December 27, 2012 08:06 by uberdragon


Initial Code
function timeBetween($start,$end) {
  if (empty($start) || empty($end)) { return; }
  else return timeSince(strtotime($start),strtotime($end),2,'short');
}

/**
* Get time difference in human readable format
* $start is start time (unix time), $end is end time (unix time), $units is numeric and defines how many time units to display
*/
function timeSince($start,$end='',$units=2,$words='long') { // $start and $end should be Unix time() format
	switch($words) {
		case 'long':
			$unitName = array( // Change this array to reflect what should be displayed as unit names and pluralization append
				'year'		=> 'year',
				'month'		=> 'month',
				'week'		=> 'week',
				'day'		=> 'day',
				'hour'		=> 'hour',
				'minute'	=> 'minute',
				'second'	=> 'second',
				'plural'	=> 's'
			);
			break;
		case 'short':
			$unitName = array( // Change this array to reflect what should be displayed as unit names and pluralization append
				'year'		=> 'yr',
				'month'		=> 'mo',
				'week'		=> 'wk',
				'day'		=> 'day',
				'hour'		=> 'hr',
				'minute'	=> 'min',
				'second'	=> 'sec',
				'plural'	=> 's'
			);
			break;
	}
    // Common time periods as an array of arrays
	$periods = array(
        array(60 * 60 * 24 * 365 , $unitName['year']),
        array(60 * 60 * 24 * 30 , $unitName['month']),
        array(60 * 60 * 24 * 7, $unitName['week']),
        array(60 * 60 * 24 , $unitName['day']),
        array(60 * 60 , $unitName['hour']),
        array(60 , $unitName['minute']),
        array(1 , $unitName['second']),
    );
	$end = (!empty($end))?$end:time(); // if no end timestamp given use the current one for end date
    $since = $end - $start; // Find the difference of time between now and the past
    // Loop around the periods, starting with the biggest
    for ($i = 0, $j = count($periods); $i < $j; $i++){    
        $seconds = $periods[$i][0];
        $name = $periods[$i][1];
        // Find the biggest whole period
        if (($count = floor($since / $seconds)) != 0){
            break;
        }
    }
    $output = ($count == 1) ? '1 '.$name : "$count {$name}s";
    $deducted = ($seconds * $count);
    for($z = 1, $j = count($periods); $z < $j; $z++) {
      if ($units > $z && $i + $z < $j){
          // Retrieving the next requested relevant period
          $seconds = $periods[$i + $z][0];
          $name = $periods[$i + $z][1];
          // Only show it if it's greater than 0
          if (($count = floor(($since - $deducted) / $seconds)) != 0){
              $deducted = $deducted+($seconds * $count);
              $output .= ($count == 1) ? ', 1 '.$name : ", {$count} {$name}{$unitName['plural']}";
          }
      }
    }
    return $output;
}
/**
* Identical to timeSince except your end time should be greater than your start time
*/
function timeUntil($end,$start='',$units=2) {
	$start = (!empty($start))?$start:time();
	return timeSince($start,$end,$units);
}

Initial URL

                                

Initial Description
Get time difference in human readable format

Initial Title
timeSince() PHP function

Initial Tags

                                

Initial Language
PHP