Check whether the string is a unix timestamp

后端 未结 12 1840
挽巷
挽巷 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

    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;
    }
    

提交回复
热议问题