Created
October 7, 2015 14:26
-
-
Save rettal/6df89ce4e1ca24556eda to your computer and use it in GitHub Desktop.
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
# http://php.net/manual/en/function.checkdate.php | |
function validateDate($date, $format = 'Y-m-d H:i:s') | |
{ | |
$d = DateTime::createFromFormat($format, $date); | |
return $d && $d->format($format) == $date; | |
} | |
var_dump(validateDate('2012-02-28 12:12:12')); # true | |
var_dump(validateDate('2012-02-30 12:12:12')); # false | |
var_dump(validateDate('2012-02-28', 'Y-m-d')); # true | |
var_dump(validateDate('28/02/2012', 'd/m/Y')); # true | |
var_dump(validateDate('30/02/2012', 'd/m/Y')); # false | |
var_dump(validateDate('14:50', 'H:i')); # true | |
var_dump(validateDate('14:77', 'H:i')); # false | |
var_dump(validateDate(14, 'H')); # true | |
var_dump(validateDate('14', 'H')); # true |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment