Created
February 17, 2015 21:35
-
-
Save mca-gif/9adfb23f1b97c8313f84 to your computer and use it in GitHub Desktop.
Regular Expression : Date in MM/DD/YYYY
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Match the following formats: | |
* mm/dd/yyyy | |
* m/d/yyyy | |
* mm/d/yyyy | |
* m/dd/yyyy | |
*/ | |
$date_regex = "/^(1[0-2]|0{0,1}[1-9])\/(3[0-1]|[0-2]{0,1}[1-9])\/[0-9]{4,4}$/"; | |
$input = array( | |
"01/01/1111" => 1, | |
"1/01/1111" => 1, | |
"01/1/1111" => 1, | |
"1/1/1111" => 1, | |
"12/31/9999" => 1, | |
"12/31/0000" => 1, | |
"31/12/9999" => 0, | |
"13/31/9999" => 0, | |
"12/32/9999" => 0, | |
"00/01/9999" => 0, | |
"01/00/9999" => 0, | |
"27/02/1234" => 0, | |
"12/40/1234" => 0, | |
"m/d/yyyy" => 0, | |
"mm/dd/yyyy" => 0 | |
); | |
foreach ( $input as $test => $result ) { | |
print "${test} = ${result} ... "; | |
print (preg_match($date_regex, $test) == $result) ? "passed" : "failed"; | |
print "\n"; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment