All pastes #882173 Raw Edit

Anonymous

public php v1 · immutable
#882173 ·published 2008-01-30 01:41 UTC
rendered paste body
<?phpfunction date_diff ($t1 = null, $t2 = null, $precision = 6, $abbr = false) {    if ($t1 === null)        $t1 = time();    elseif (preg_match('/\D/', $t1) && ($t1 = strtotime($t1)) === false)        return false;    if ($t2 === null)        $t2 = time();    elseif (preg_match('/\D/', $t2) && ($t2 = strtotime($t2)) === false)        return false;    // make sure that $t2 > $t1    if ($t1 > $t2)        list($t1, $t2) = array($t2, $t1);    $diffs = array(        's' => array('second', 'sec', 0),        'i' => array('minute', 'min', 0),        'G' => array('hour', 'hr', 0),        'j' => array('day', 'day', 0),        'n' => array('month', 'mnth', 0),        'Y' => array('year', 'yr', 0)    );    foreach ($diffs as $key => &$diff) {        $s2 = (int)date($key, $t2);        while ( ($s1 = (int)date($key, $t1)) !== $s2 ) {            $t1 = strtotime("+1 {$diff[0]}", $t1);            ++$diff[2];        }    }    unset($diff);    $stack = array();    foreach ($diffs as $diff)        array_unshift($stack, array($diff[2], ($abbr ? $diff[1] : $diff[0]) . ($diff[2] == 1 ? '' : 's')));    while (count($stack) > 0 && $stack[0][0] == 0)        array_splice($stack, 0, 1);    array_splice($stack, $precision);    $ret = array();    foreach ($stack as $item)        $ret[] = "{$item[0]} {$item[1]}";    return implode(', ', $ret);}var_dump(date_diff('2008-01-29 18:09:06', time(), 3, true));?>