• PHP Video Tutorials

PHP checkdate() Function


Advertisements

Definition and Usage

The checkdate() function accepts the month, day, year of a date as parameters and, verifies whether it is a Gregorian date or not.

Syntax

checkdate ( int $month , int $day , int $year )

Parameters

Sr.No Parameter & Description
1

month

This is an integer value representing the month of a date, it must be between between 1 and 12.

2

day

This is an integer value representing the day of a date, it must be below the allowed number of days in the given month.

3

year

This is an integer value representing the year of a date, it must be between between 1 and 32767.

Return Values

PHP checkdate() function returns a boolean value. This value is true if the given date is valid and, false if it is invalid.

PHP Version

This function was first introduced in PHP Version 4, and works with all the later versions.

Example

Following example demonstrates the usage of the checkDate() function −

<?php
   var_dump(checkdate(11, 07, 1989));
   var_dump(checkdate(02, 31, 2008));
   
   $bool = (checkdate(06, 03, 1889));
   print($bool);
   print("\n");
   print("result: ".checkdate(13, 30, 2005));
?>

This will produce following result −

bool(true)
bool(false)
1
result:

Example

In this example, we are trying to verify the dates of leap year(s)−

<?php
   var_dump(checkdate(02, 30, 2004));
   var_dump(checkdate(02, 28, 2008));   
   var_dump(checkdate(05, 31, 2020));
   var_dump(checkdate(06, 31, 2020));
?>

This will produce following result −

bool(false)
bool(true)
bool(true)
bool(false)

Example

Following example verifies whether the date 12/12/2005 is Gregorian, or not −

<?php
   $bool = checkdate(12, 12, 2005);
   
   if($bool){
      print("Given date is valid");
   }else{
      print("Given date is invalid");
   }
?>

This will produce following result −

Given date is valid
php_function_reference.htm
Advertisements