Skip to content

Instantly share code, notes, and snippets.

@drakakisgeo
Created September 25, 2012 18:45
Show Gist options
  • Save drakakisgeo/3783655 to your computer and use it in GitHub Desktop.
Save drakakisgeo/3783655 to your computer and use it in GitHub Desktop.
Checks value IF is a valid date
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;
}
}
}
@ChrisMorrisCo
Copy link

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