php人性化显示,几秒前,几天前,昨天,和日期

function formatTimeHuman($time){
    if(!is_numeric($time)){
        $timeOreign = strtotime($time);
    }

    $todayTime = strtotime(date('Y-m-d'));

    $time = time() - $timeOreign;
    if ($time < 60){
        $str = '刚刚';
    }elseif($time < 60 * 60){
        $min = floor($time/60);
        $str = $min.'分钟前';
    }elseif($timeOreign >  $todayTime){
        $h = floor($time/(60*60));
        $str = $h.'小时前';
    }elseif($timeOreign > $todayTime - 86400 ){
        $str = '昨天';
    }else{
        if(date("Y", $timeOreign) == date("Y", time())){
            $str = date("n月j日",$timeOreign);
        }else{
            $str = date("Y年m月d日",$timeOreign);
        }
    }
    return $str;
}

你可能感兴趣的:(php人性化显示,几秒前,几天前,昨天,和日期)