PHP | checkdate() Function


The checkdate() function is a built-in function in PHP which checks the validity of the date passed in the arguments. It accepts the date in the format mm/dd/yyyy. The function returns a boolean value. It returns true if the date is a valid one, else it returns false.

Syntax:

checkdate ( $month, $day, $year )

Parameters: The function accepts three mandatory parameters as shown above and described below:

  1. $month – This parameter specifies the month. The month has to be in between 1 to 12 for a valid date.
  2. $day – This parameter specifies the day. The day can be in range 1-31 depending on the month entered for it to be a valid day. In case of a leap year, the day is in range 1-29 and for a non-leap year the day is in range 1-28.
  3. $year – This parameter specifes the year. The year has to be in range 1-32767 inclusive depending on the $month and $day for it to be a valid date.

Return Value: The function returns a boolean value. It returns true if the passed date is a valid date. It returns false if the passed date is not a valid one.

Examples:



Input : $month = 12 $day = 31 $year = 2017
Output : true

Input : $month = 2 $day = 29 $year = 2016
Output : true 

Input : $month = 2 $day = 29 $year = 2017
Output : false

Below programs illustrate the checkdate() function in PHP :

Program 1: The program below check if the date is a valid one or not.

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to demonstrate the checkdate() function 
  
$month = 12; 
$day = 31; 
$year = 2017; 
  
// returns a boolean value after validation of date 
var_dump(checkdate($month, $day, $year));
  
?> 

chevron_right


Output:

bool(true)

Program 2: The program below check if the date is a valid one or not in case of a leap year and non-leap year.

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to demonstrate the checkdate() function 
// in case of leap year
  
$month = 2; 
$day = 29; 
$year = 2016; 
  
// returns a boolean value after validation of date
// leap year 
var_dump(checkdate($month, $day, $year)); 
  
  
$month = 2; 
$day = 29; 
$year = 2017; 
  
// returns a boolean value after validation of date
// non-leap year 
var_dump(checkdate($month, $day, $year)); 
  
?> 

chevron_right


Output:

bool(true)
bool(false)



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.