php自定义函数: 时间转换成智能形式

时间:2024-07-24 19:37:38
function time_trans($paratime,$suffix=false){
    $now_time = time();
    $dur = $now_time - $paratime;
    $suffix && $sustr = '前';
    if($dur < 0){
        return $time;
    }else{
        if($dur < 60){
            return $dur.'秒'.$sustr;
        }else{
            if($dur < 3600){
                return floor($dur/60).'分钟'.$sustr;
            }else{
                if($dur < 86400){
                    return floor($dur/3600).'小时'.$sustr;
                }else{
                    if($dur < 259200){//3天内
                        return floor($dur/86400).'天'.$sustr;
                    }else{
                        return $time;
                    }
                }
            }
        }
    }
}