Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PHP

Multiple Formats with PHP DateTime::createFromFormat()

$date = '20200727';
$formats = ['Y-m-d', 'Y/m/d', 'Ymd'];
$dateObj = null;
foreach ($formats as $format) {
    try{
        if ($dateObj = Carbon::createFromFormat($format, $date)){
            break;   
        }
    } catch (InvalidArgumentException $e) {
        Log::debug('date', [$dateObj]); // It's null
    }
}
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #Multiple #Formats #PHP
ADD COMMENT
Topic
Name
4+4 =