php几秒几天前,时间转换

源码 · 2022-12-03 · 291 人浏览
/**
 * 几秒几天前,时间转换
 * @return string
 */
if (!function_exists('format_time_ago')) {
    function format_time_ago($date)
    {
        if(is_numeric($date)){
            $timer = $date;
        }else{
            $timer = strtotime($date);
        }
        $diff = $_SERVER['REQUEST_TIME'] - $timer;
        $day = floor($diff / 86400);
        $free = $diff % 86400;
        if ($day > 0) {
            return $day . "天前";
        } else {
            if ($free > 0) {
                $hour = floor($free / 3600);
                $free = $free % 3600;
                if ($hour > 0) {
                    return $hour . "小时前";
                } else {
                    if ($free > 0) {
                        $min = floor($free / 60);
                        $free = $free % 60;
                        if ($min > 0) {
                            return $min . "分钟前";
                        } else {
                            if ($free > 0) {
                                return $free . "秒前";
                            } else {
                                return '刚刚';
                            }
                        }
                    } else {
                        return '刚刚';
                    }
                }
            } else {
                return '刚刚';
            }
        }
    }
}
豫ICP备17047052号-1