php validate date format

Solutions on MaxInterview for php validate date format by the best coders in the world

showing results for - "php validate date format"
Yohan
20 Mar 2020
1$date="2012-09-12";
2
3if (preg_match("/^(20[0-9]{2})-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])$/",$date)) {
4    return true;
5} else {
6    return false;
7}
8
Antonio
12 Apr 2019
1function isValidDate(string $date, string $format = 'Y-m-d'): bool
2{
3    $dateObj = DateTime::createFromFormat($format, $date);
4    return $dateObj && $dateObj->format($format) == $date;
5}
6