日期时间与当前相比是多久前

    /**
     * 日期时间与当前相比是多久前
     * @param $time
     * @return string
     */
    public function getCreateTimeAttr($time)
    {
        $nowTime = time();
        $difference = $nowTime - $time;
        if ($difference < 0) {
            return $time;
        }
        if ($difference < 60) {
            return $difference . '秒前';
        }
        if ($difference < 3600) {
            return floor($difference / 60).'分钟前';
        }
        if ($difference < 86400) {
            return floor($difference / 3600).'小时前';
        }
        if ($difference < 2592000)  {
            return floor($difference / 86400) . '天前'; //30天内
        }
        if ($difference < 31104000) {
            return floor($difference / 2592000) . '个月前'; //12个月内
        }
        return floor($difference / 31536000) . '年前';
    }

 

你可能感兴趣的:(日期时间与当前相比是多久前)