PHP Trick: Date Validation
Following code first checks if the date expression is ok, then evaluates the date through checkdate() function:
$dt = "3-31-1921";
$expr = "/\b(0?[1-9]|1[012])[-](0?[1-9]|[12][0-9]|3[01])[-](19|20)\d\d\b/";
if ( preg_match($expr, $dt, $match) ){
$expr = "/\b(0?[1-9]|1[012])[-](0?[1-9]|[12][0-9]|3[01])[-](19|20)\d\d\b/";
if ( preg_match($expr, $dt, $match) ){
list($m, $d, $y) = explode('-', $dt);
if( checkdate($m, $d, $y) )
echo "valid date.";
else
echo "invalid date.";
} else
echo "not a date expression.";
if( checkdate($m, $d, $y) )
echo "valid date.";
else
echo "invalid date.";
} else
echo "not a date expression.";