计算过去多少时间

/**
 * 计算过去多少时间 $time 传入的时间戳
 * @route('UserExercises/to_time')
 */
public function to_time($time)
{
    $t = time() - $time == 0 ? 1 : time() - $time;
    $f = array(
        '31536000' => '年',
        '2592000' => '个月',
        '604800' => '星期',
        '86400' => '天',
        '3600' => '小时',
        '60' => '分钟',
        '1' => '秒'
    );
    foreach ($f as $k => $v) {
        if (0 != $c = floor($t / (int)$k)) {
            return $c . $v . '前';
        }
    }
}

你可能感兴趣的:(php计算)