Open In App
Related Articles

PHP | checkdate() Function

Improve Article
Improve
Save Article
Save
Like Article
Like

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 specifies 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. 

php




<?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));
 
?>


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.  

php




<?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));
 
?>


Output:  

bool(true)
bool(false)

PHP is a server-side scripting language designed specifically for web development. You can learn PHP from the ground up by following this PHP Tutorial and PHP Examples.


Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Last Updated : 10 Sep, 2021
Like Article
Save Article
Similar Reads
Related Tutorials