I\'m receiving a date string from an API, and it is formatted as yyyy-mm-dd
.
I am currently using a regex to validate the string format, which works ok,
How about this one?
We simply use a try-catch block.
$dateTime = 'an invalid datetime';
try {
$dateTimeObject = new DateTime($dateTime);
} catch (Exception $exc) {
echo 'Do something with an invalid DateTime';
}
This approach is not limited to only one date/time format, and you don't need to define any function.