PHP Program to check whether the date is valid or not using the checkdate() function


March 22, 2022, Learn eTutorial
1499

How to check whether the entered date is a valid date or not using PHP?

In this program, we are checking whether the user entered date is a valid date or not using the built-in function checkdate(). First, we accept the date, month, and the year from the user and assign them to the variables s, m, and y after that we assign the result of the built-in function to check the date is valid or not checkdate(m,d, y) into the variable date. Then we check the value of the variable date if true the print the entered date as a valid date otherwise print the entered date is not a valid date. For example, if the user entered values are

date = 30

m>

year = 2021

As we all know that the month of February has only 28 or 29 days so we can say that the entered date is not a valid date.

Syntax of checkdate() function


checkdate(month, day, year)
 

ALGORITHM

Step 1: Accept the date, month, and year from the user and assign it to the variables d, m, and y

Step 2: Assign the result of the built-in function to check the date is valid or not checkdate(m,d, y) into the variable date

Step 3: Check the value of the variable date if true the print the entered date as a valid date otherwise print the entered date is not a valid date

PHP Source Code

                                          <?php
$d = readline("Enter the date: ");
$m = readline("Enter the month: ");
$y = readline("Enter the year: ");
$date = checkdate($m, $d, $y);
if ($date)
    echo "The entered date $d-$m-$y is a valid date";
else
    echo "The entered date $d-$m-$y is not a valid date";
?>
                                      

OUTPUT

Example 1
Enter the date: 10
Enter the month: 11
Enter the year: 2003
The entered date 10-11-2003 is a valid date

Example 2
Enter the date: 23
Enter the month: 20
Enter the year: 2020
The entered date 23-20-2020 is not a valid date