If a date is submitted by form in following format, $month=2, $day=31, $year= 2010
. How can i verify using PHP date function if it is valid date or not? Thanks.
http://php.net/manual/en/function.checkdate.php
The checkdate
function is the first result in google from the search "php validate date"
In your case, the usage would be:
checkdate($month, $day, $year);
<?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
function was copied from this answer or php.net
Try checkdate() http://php.net/manual/en/function.checkdate.php
checkdate($month, $day, $year);
returns true if date is valid / false otherwise
bool checkdate ( int $month , int $day , int $year )
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With