PHP | checkdate() Function
Last Updated :
10 Sep, 2021
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:
- $month – This parameter specifies the month. The month has to be in between 1 to 12 for a valid date.
- $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.
- $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
$month = 12;
$day = 31;
$year = 2017;
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
$month = 2;
$day = 29;
$year = 2016;
var_dump( checkdate ( $month , $day , $year ));
$month = 2;
$day = 29;
$year = 2017;
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.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...