Created
February 9, 2013 20:21
-
-
Save ijmorgado/4746951 to your computer and use it in GitHub Desktop.
This is a small snippet to check if a string is a correct date.
This file contains hidden or 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
$compare = "31-10-1991"; | |
// first validate the format...it could be: 24-05-2013,24/05/2013,24.05.2013,24-5-2013,24.5.2013,24/5/2013...or something like that(at this point we've just validated the format...not the date) | |
if(preg_match('/^(3[0-1]|[0-2]?[0-9])[\/.-](1[0-2]|0?[0-9])[\/.-][0-9]{4}$/',$compare)){ | |
$date_parsed = preg_split('/[\/.-]/', $compare); | |
//if the format is correct we're going to see if the date is valid....for example: 31-02-2013 should be invalid.... | |
if(!checkdate($date_parsed[1],$date_parsed[0],$date_parsed[2])){ | |
echo "invalid"; | |
// ...whatever you want to do if is invalid .... | |
}else{ | |
echo "valid"; | |
// ...whatever you want to do if is valid .... | |
} | |
}else{ | |
echo "invalid"; | |
// ...whatever you want to do if is invalid .... | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment