PHP 判斷幾秒前,幾分鐘,幾小時前

 

PHP 對於時間的過了多久的判斷,幾秒前,幾分鐘前,幾小時前,$time = strtotime("2017-01-15 14:42:00");spa

 

$time = strtotime("2017-01-15 16:42:00");
echo ReckonTime($time);

function ReckonTime($time)
{
    $NowTime = time();
    if($time > $NowTime){ return false; }
    $TimePoor = $NowTime - $time;
    if ($TimePoor == 0) {
        $str = '一眨眼之間';
    } else if ($TimePoor < 60 && $TimePoor > 0) {
        $str = $TimePoor . '秒以前';
    } else if ($TimePoor >= 60 && $TimePoor <= 60 * 60) {
        $str = floor($TimePoor / 60) . '分鐘前';
    } else if ($TimePoor > 60 * 60 && $TimePoor <= 3600 * 24) {
        $str = floor($TimePoor / 3600) . '小時前';
    } else if ($TimePoor > 3600 * 24 && $TimePoor <= 3600 * 24 * 7) {
        if (floor($TimePoor / (3600 * 24)) == 1) {
            $str = "昨天";
        } else if (floor($TimePoor / (3600 * 24)) == 2) {
            $str = "前天";
        } else {
            $str = floor($TimePoor / (3600 * 24)) . '天前';
        }
    } else if ($TimePoor > 3600 * 24 * 7) {
        $str = date("Y-m-d", $time);
    }
    return $str;
}

 

 

 

if($time > $NowTime){ return false; }
相關文章
相關標籤/搜索