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.
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 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.
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.
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.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.