Check whether the string is a unix timestamp

后端 未结 12 1735
挽巷
挽巷 2020-11-27 16:43

I have a string and I need to find out whether it is a unix timestamp or not, how can I do that effectively?

I found this thread via Google, but it doesn\'t come up

相关标签:
12条回答
  • 2020-11-27 17:01

    You want to check if a string contains a high number?

    is_numeric() is the key

    Or convert it to DateTime and do some checks with it like an expected date range.

    0 讨论(0)
  • 2020-11-27 17:02

    I came across the same question and created the following solution for my self, where I don't have to mess with regular expressions or messy if-clauses:

    /**
     * @param string $string
     * @return bool
     */
    public function isTimestamp($string)
    {
        try {
            new DateTime('@' . $string);
        } catch(Exception $e) {
            return false;
        }
        return true;
    }
    
    0 讨论(0)
  • 2020-11-27 17:02

    this looks like the way to go:

    function is_timestamp($timestamp) {
        if(strtotime(date('d-m-Y H:i:s',$timestamp)) === (int)$timestamp) {
            return $timestamp;
        } else return false;
    }
    

    you could also add a is_numeric() check and all sort of other checks.
    but this should/could be the basics.

    0 讨论(0)
  • 2020-11-27 17:03

    Another possibility:

    $date_arg = time();
    $date_is_ok = ($date_arg === strtotime(date('c', $date_arg)));
    
    0 讨论(0)
  • 2020-11-27 17:06

    Ok, after fiddling with this for some time, I withdraw the solution with date('U') and suggest to use this one instead:

    function isValidTimeStamp($timestamp)
    {
        return ((string) (int) $timestamp === $timestamp) 
            && ($timestamp <= PHP_INT_MAX)
            && ($timestamp >= ~PHP_INT_MAX);
    }
    

    This check will only return true if the given $timestamp is a string and consists solely of digits and an optional minus character. The number also has to be within the bit range of an integer (EDIT: actually unneeded as shown here).

    var_dump( isValidTimeStamp(1)             ); // false
    var_dump( isValidTimeStamp('1')           ); // TRUE
    var_dump( isValidTimeStamp('1.0')         ); // false
    var_dump( isValidTimeStamp('1.1')         ); // false
    var_dump( isValidTimeStamp('0xFF')        ); // false
    var_dump( isValidTimeStamp('0123')        ); // false
    var_dump( isValidTimeStamp('01090')       ); // false
    var_dump( isValidTimeStamp('-1000000')    ); // TRUE
    var_dump( isValidTimeStamp('+1000000')    ); // false
    var_dump( isValidTimeStamp('2147483648')  ); // false
    var_dump( isValidTimeStamp('-2147483649') ); // false
    

    The check for PHP_INT_MAX is to ensure that your string can be used correctly by date and the likes, e.g. it ensures this doesn't happen*:

    echo date('Y-m-d', '2147483648');  // 1901-12-13
    echo date('Y-m-d', '-2147483649'); // 2038-01-19
    

    On 64bit systems the integer is of course larger than that and the function will no longer return false for "2147483648" and "-2147483649" but for the corresponding larger numbers.


    (*) Note: I'm not 100% sure, the bit range corresponds with what date can use though

    0 讨论(0)
  • 2020-11-27 17:13

    Improved answer to @TD_Nijboer.

    This will avoid an exception be thrown if the supplied string is not a time stamp:

    function isTimestamp($timestamp) {
        if(ctype_digit($timestamp) && strtotime(date('Y-m-d H:i:s',$timestamp)) === (int)$timestamp) {
            return true;
        } else {
            return false;
        }
    }
    
    0 讨论(0)
提交回复
热议问题