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?
In PHP, regular expressions are strings composed of delimiters, a pattern and optional modifiers. $exp = "/w3schools/i"; In the example above, / is the delimiter, w3schools is the pattern that is being searched for, and i is a modifier that makes the search case-insensitive.
^(19|20)\d\d[- /.] (0[1-9]|1[012])[- /.] (0[1-9]|[12][0-9]|3[01])$ matches a date in yyyy-mm-dd format from 1900-01-01 through 2099-12-31, with a choice of four separators.
The problem is one of delimeters and escaped characters (as others have mentioned). This will work:
$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'; }
Note that I added a forward-slash to the beginning and ending of the expression and escapped (with a back-slash) the forward-slashes in the pattern.
To take it one step further, this pattern won't properly extract the year... just the century. You'd need to change it to /(0[1-9]|1[012])[- \/.](0[1-9]|[12][0-9]|3[01])[- \/.]((?:19|20)\d\d)/
and (as Jan pointed out below) if you want to make sure the whole string matches (instead of some subset) you'll want to go with something more like /^(0[1-9]|1[012])[- \/.](0[1-9]|[12][0-9]|3[01])[- \/.]((?:19|20)\d\d)$/
.
As others have mentioned, strtotime() might be a better option if you're just trying to get the date out. It can parse almost any commonly used format and it will give you a unix timestamp. You can use it like this:
$test_date = '03/22/2010'; // get the unix timestamp for the date $timestamp = strtorime($test_date); // now you can get the date fields back out with one of the normal date/time functions. example: $date_array = getdate($timestamp); echo 'the month is: ' . $date_array['month'];
Regex of the author of the question is almost correct. Date is not validated because a pattern should be:
pattern="^(0[1-9]|1[012])[/](0[1-9]|[12][0-9]|3[01])[/](19|20)\d\d$"
or if to be fancy:
pattern="^(0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])[- /.](19|20)\d\d$"
Please note that this pattern checks only for a format of a date and max/min values for individual date elements. That means a user's entry should be checked for validity of date in JavaScript function and/or a Server (e.g February 29 should not be allowed if a year is not a leap one).
As a side note, if you would like to allow a single digit (let's say for a month), change a month part to
([0-9]|0[1-9]|1[012])
Explanation:
[0-9] - single digit between 0 and 9
or
0[1-9] - two digits between 01 and 09
or
1[012] - two digits limited to 10, 11, 12
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With