-
-
Save drakakisgeo/3783655 to your computer and use it in GitHub Desktop.
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; | |
} | |
} | |
} |
Thank you Chris :-) I also didn't knew about it, its applied only in > PHP 5.3 version
Some brackets are missing but I got your point.
Ah, okay. Maybe I'm a bit rusty on my PHP :P. Great feature though, makes live easier.
The brackets are optional if you're only running one statement after an if statement.
Example:
if($a == 1)
echo "true";
is equivalent to
if($a == 1){
echo "true";
}
However, the following would require brackets:
if($a == 1)
echo "true";
echo "something else";
The above example would always echo "something else", but only echo "true" if $a == 1. If statements without brackets only run the first statement after them.
If you want to see the difference, try this code out:
<?php
$a = 1;
if($a == 1)
echo "A is 1";
echo "This is not part of the bracket-less if statement.";
if($a == 1){
echo "A is 1";
echo "This is part of the if statement as brackets will allow for more than one statement to be executed";
}
?>
Run the above and try switching $a from 1 to 0, and you'll get the idea :). Sometimes it may be neater not to use brackets, but it's really just a personal preference. It's also easier to make a mistake when not using brackets.
I haven't tried this, but you might be able to simplify a little further.
public static function is_date($date){
// date example mm-dd-year -> 09-25-2012
$datechunks = explode("-",$date);
if((sizeof($datechunks)==3)
return checkdate($datechunks[0], $datechunks[1], $datechunks[2]);
return false;
}
The above will first explode the input, and if the number of items in the array is 3, then it will check to see if it's a date - otherwise return false. If there are 3 items in the array, then it will return the value of checkdate() which should either return true or false. I think checkdate() takes integers for input, so you may not require the is_numeric() checks. I could be wrong, though.
The above code is the same as this, if you prefer brackets:
public static function is_date($date){
// date example mm-dd-year -> 09-25-2012
$datechunks = explode("-",$date);
if((sizeof($datechunks)==3){
return checkdate($datechunks[0], $datechunks[1], $datechunks[2]);
}
return false;
}
Anyway, I just thought I'd share :).
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.
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.
There we go.