Created
September 25, 2012 18:45
-
-
Save drakakisgeo/3783655 to your computer and use it in GitHub Desktop.
Checks value IF is a valid date
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
function is_date($date){ | |
// date example mm-dd-year -> 09-25-2012 | |
$datechunks = explode("-",$date); | |
if(sizeof($datechunks)==3){ | |
if(is_numeric($datechunks[0]) && is_numeric($datechunks[1]) && is_numeric($datechunks[2])) | |
{ | |
// now check if its a valid date | |
if(checkdate($datechunks[0], $datechunks[1], $datechunks[2])){ | |
return true; | |
}else{ | |
return false; | |
} | |
}else{ | |
return false; | |
} | |
} | |
} |
As for the brackets, I was troubled about the sequence return true; return false; you had in your code. Anyway later tonight I'll refactor it and test it and try to shorten it as much as I can :-)
Ah k, fair enough :).
The return statement automatically exits the function once reached. My reordering of the return statements simply saves if/else lines.
For example:
if ($x == 1) {
return false;
} else {
return true;
}
Is the same as:
if ($x != 1)
return true;
return false;
Because the first return statement has been reached, there's no need to go any further in the function, so PHP will ignore everything below "return true;" (should $x not equal 1, that is.) Otherwise, if $x is 1, it will return false because the "return true;" statement is skipped.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I already tried that and you get an error in case any value isn't numeric. So you have to check each (day/month/year) if its a valid number and the go for checkdate function.