Date regular expression gives error
I have been working on my date regular 开发者_StackOverflowexpression all day... I want a date format to be YYYY-MM-DD.
$date_regex ='^(19|20)\d\d[- /.](0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])$';
if (preg_match($date_regex, $dateString)) {
echo "good format";
}
keeps giving me error
preg_match() [function.preg-match]: No ending delimiter '^' found in test.php on line 19
Anyone help?? Thanks a lot!!
You need to wrap your regular expression in a pair of delimiting characters. Also, you need to escape the dashes in your character classes using \
.
Try this (I'm using #
as a delimiter):
$date_regex ='#^(19|20)\d\d[\- /.](0[1-9]|1[012])[\- /.](0[1-9]|[12][0-9]|3[01])$#';
You need to include the /s otherwise the regex thinks you are limiting it with the ^
'/^(19|20)\d\d[- /.](0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])$/'
Regards
精彩评论