Created
August 11, 2017 16:09
-
-
Save hughshen/7ede4aeafdaa303f448118091a7309ce to your computer and use it in GitHub Desktop.
With DateTime you can make the shortest date&time validator for all formats. http://php.net/manual/en/function.checkdate.php#113205
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
<?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 | |
var_dump(validateDate('2012-02-28T12:12:12+02:00', 'Y-m-d\TH:i:sP')); # true | |
# or | |
var_dump(validateDate('2012-02-28T12:12:12+02:00', DateTime::ATOM)); # true | |
var_dump(validateDate('Tue, 28 Feb 2012 12:12:12 +0200', 'D, d M Y H:i:s O')); # true | |
# or | |
var_dump(validateDate('Tue, 28 Feb 2012 12:12:12 +0200', DateTime::RSS)); # true | |
var_dump(validateDate('Tue, 27 Feb 2012 12:12:12 +0200', DateTime::RSS)); # false | |
# ... |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment