Check whether the string is a unix timestamp

后端 未结 12 1842
挽巷
挽巷 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: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.

提交回复
热议问题