php时间差计算 时间美化 几分钟前 几小时前 几天前

时间美化函数1.0

	/**
     * [format_date 时间美化函数v1.0]
     */
    public function format_date($time)
    {
        $t = 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.'前';
            }
        }
    }

时间美化函数2.0

	/**
	 * [format_time 时间美化函数v2.0]
	 */
	public function format_time($time)
	{
		$todayLast = strtotime(date('Y-m-d').' 23:59:59');
		$agoTimeTrue = time() - $time;
		$agoTime = $todayLast - $time;
		$agoDay = floor($agoTime / 86400);
		$res = '';
		if ($agoTimeTrue < 60) {
			$res = '刚刚';
		}elseif ($agoTimeTrue < 3600) {
			$res = (ceil($agoTimeTrue / 60)).'分钟前';
		}elseif ($agoTimeTrue < (3600 * 12)) {
			$res = (ceil($agoTimeTrue / 3600)).'小时前';
		}elseif ($agoDay == 0) {
			$res = '今天 '.date('H:i',$time);
		}elseif ($agoDay == 1) {
			$res = '昨天 '.date('H:i',$time);
		}elseif ($agoDay == 2) {
			$res = '前天 '.date('H:i',$time);
		}elseif (($agoDay > 2) && ($agoDay < 16)) {
			$res = $agoDay.'天前'.date('H:i',$time);
		}else{
			$res = date('Y-m-d H:i:s',$time);
		}
		return $res;
	}
    	$time // 传入参数  时间戳

猜你喜欢

转载自blog.csdn.net/qq_15957557/article/details/104497280