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; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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:
Is the same as:
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.