PHP--判斷是否爲時間戳

PHP 中判斷一個數字是不是Unix 時間戳

在php中一個有效的時間是從 1970-01-01 07:00:00 – 2038-01-19 03:14:07. 這個的http://php.net/manual/en/function.strtotime.php
首先一個時間戳中確定沒有小數點.
將 1970-01-01 07:00:00 和 2038-01-19 03:14:07轉換成時間戳.php

  1. echo strtotime('2038-01-19 03:14:07'); // 2147454847
    echo strtotime('1970-01-01 07:00:00'); // 0
    $valid = ctype_digit($str) && $str <= 2147483647;
    var_dump($valid);
     
    //method 2
    function is_timestamp($timestamp) {
        if(strtotime(date('m-d-Y H:i:s',$timestamp)) === $timestamp) {
            return $timestamp;
        } else return false;
    }
    
相關文章
相關標籤/搜索