Check whether the string is a unix timestamp

后端 未结 12 1825
挽巷
挽巷 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 16:51

        //if anything else than digits inside the string then your string is no timestamp 
        //in which case maybe try to get the timestamp with strtotime
    
        if(preg_match('/[^\d]/', $str)) {
            $str = strtotime($str);
    
            if (false === $str) {
                //conversion failed - invalid time - invalid row
                return;
            }
        }
    

提交回复
热议问题