The checkdate() function can validate a Gregorian date.
PHP checkdate() Function has the following syntax.
checkdate(month,day,year);
Parameter | Is Required | Description |
---|---|---|
month | Required. | Month value as a number between 1 and 12 |
day | Required. | Day value as a number between 1 and 31 |
year | Required. | Year value as a number between 1 and 32767 |
PHP checkdate() Function returns TRUE if the date is valid. FALSE otherwise.
Validate a Gregorian date
<?php
var_dump(checkdate(2,29,2004));
var_dump(checkdate(2,31,2004));
?>
The code above generates the following result.
The following code shows how to check if a date value is valid.
<?php//from w w w. j a va 2 s. c o m
echo "April 31, 2010: ".(checkdate(4, 31, 2010) ? 'Valid' : 'Invalid');
// Returns false, because April only has 30 days
echo "February 29, 2012: ".(checkdate(02, 29, 2012) ? 'Valid' : 'Invalid');
// Returns true, because 2012 is a leap year
echo "February 29, 2011: ".(checkdate(02, 29, 2011) ? 'Valid' : 'Invalid');
// Returns false, because 2011 is not a leap year
?>
The code above generates the following result.