How to determine if value is a date in PHP

后端 未结 3 1339
不思量自难忘°
不思量自难忘° 2021-02-13 21:34

I am working with arrays of values in PHP. Some of these values may include a date in various string formats.

I need to convert dates in multiple formats to their numeri

3条回答
  •  梦毁少年i
    2021-02-13 22:03

    Extrapolating from http://au1.php.net/checkdate#113205 ; just change the $formats array to all the formats you want to check.

    public function convertDate($value) {
    
        $formats = ['M d, Y', 'Y-m-d'];
        foreach($formats as $f) {
            $d = DateTime::createFromFormat($f, $value);
            $is_date = $d && $d->format($f) === $value;
    
            if ( true == $is_date ) break;
        }
    
        return $is_date;
    
    }
    

提交回复
热议问题