PHP Tutorial - PHP checkdate() FunctionCheck if several dates are valid Gregorian dates:






Definition

The checkdate() function can validate a Gregorian date.

Syntax

PHP checkdate() Function has the following syntax.

checkdate(month,day,year);

Parameter

ParameterIs RequiredDescription
monthRequired.Month value as a number between 1 and 12
dayRequired.Day value as a number between 1 and 31
yearRequired.Year value as a number between 1 and 32767

Return

PHP checkdate() Function returns TRUE if the date is valid. FALSE otherwise.





Example

Validate a Gregorian date


<?php
var_dump(checkdate(2,29,2004));
var_dump(checkdate(2,31,2004));
?>

The code above generates the following result.

Example 2

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.