Skip to content

Instantly share code, notes, and snippets.

@mca-gif
Created February 17, 2015 21:35
Show Gist options
  • Save mca-gif/9adfb23f1b97c8313f84 to your computer and use it in GitHub Desktop.
Save mca-gif/9adfb23f1b97c8313f84 to your computer and use it in GitHub Desktop.
Regular Expression : Date in MM/DD/YYYY
<?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