Created
January 31, 2014 13:45
-
-
Save Kcko/8732309 to your computer and use it in GitHub Desktop.
Validation date & time
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 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