I want to validate the date format YYYY-MM-DDin PHP. But something is wrong and I can’t find it? The regular expression keeps telling me the format is wrong, when it is right.
$datePattern = '/^(2[0-9][0-9][0-9])\-(0[1-9]|1[0-2]])\-(0[1-9]|1[0-9]|2[0-9]|3[0-1])$/';
$date = '2011-11-05';
if(!preg_match($datePattern,$date)){
die('Wrong format in date!');
}
You have an extra
]in your regular expression (month subexpression) that is not needed. So it should be