What is the MM/DD/YYYY regular expression and how do I use it in php?

Posted by zeckdude on Stack Overflow See other posts from Stack Overflow or by zeckdude
Published on 2010-03-26T01:31:15Z Indexed on 2010/03/26 1:33 UTC
Read the original article Hit count: 267

Filed under:
|
|

I found the regular expression for MM/DD/YYYY at http://www.regular-expressions.info/regexbuddy/datemmddyyyy.html but I don't think I am using it correctly.

Here's my code:

$date_regex = '(0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])[- /.](19|20)\d\d';

$test_date = '03/22/2010';
if(preg_match($date_regex, $test_date)) {
  echo 'this date is formatted correctly';  
} else {
  echo 'this date is not formatted correctly';  
}

When I run this, it still echoes 'this date is not formatted correctly', when it should be saying the opposite. How do I set this regular expression up in php?

© Stack Overflow or respective owner

Related posts about php

Related posts about regex