All pastes #883158 Raw Edit

Stuff

public php v1 · immutable
#883158 ·published 2008-01-30 05:04 UTC
rendered paste body
function date_diff ($t1, $t2 = null, $precision = 6, $abbr = false) {	$abbrs = array(		'year' => 'yr', 'month' => 'mnth', 'day' => 'day',		'hour' => 'hr', 'minute' => 'min', 'second' => 'sec'	);	if (preg_match('/\D/', $t1) && ! ($t1 = strtotime($t1)))		return false;	if ($t2 === null)		$t2 = time();	elseif (preg_match('/\D/', $t2) && ! ($t2 = strtotime($t2)))		return false;	if ($t1 > $t2)		list($t1, $t2) = array($t2, $t1);	$stack = array();	foreach (array_keys($abbrs) as $interval) {		$num = 0;		while ($t2 > ($t3 = strtotime("+1 ${interval}", $t1)) && ++$num)			$t1 = $t3;		$stack[] = array($num, ($abbr ? $abbrs[$interval] : $interval) . ($num != 1 ? 's' : ''));	}	$ret = array();	while (count($ret) < $precision && ($interval = array_shift($stack)) !== null) {		if ($interval[0] > 0)			$ret[] = "{$interval[0]} {$interval[1]}";	}	return implode(', ', $ret);}var_dump(date_diff('2005-01-29 23:45:00', time(), 3, true));