PHP Program to check whether the year is leap year or not


April 17, 2022, Learn eTutorial
2065

What is a leap year?

Leap year is a year that has 366 days in it. Practically, we understand that is to check the month February has 28 days in it. For example, 2004 is a Leap year and 2005 is a common year.

How to check whether the year is a leap year or not using PHP?

In this program, we use a user-defined function to check whether the user entered year is a leap year or not. To check that the year is leap year we will check that the year entered is divisible by 4 and not divisible by 100 if these conditions are true then it will be a leap year. Then we will check that year is not divisible by 400 if it is true then it will be not a leap year in all other cases it will be a leap year. 

ALGORITHM

Step 1: Read the year from the user and assign it to the variable year

Step 2: Assign the return value of the user-defined function isLeap() with argument value of the variable year into the variable check

Step 3: Check condition if there is the value of the variable check is 1 its true print the entered year is leap year otherwise print that the entered year is not leap year

ALGORITHM User-Defined function : isLeap(year)

Step 1: Check the conditon 'year % 4 == 0' and 'year % 100 != 0' are true then return the value 1

Step 2: Check the condition 'year % 400 != 0' if the condition is true then return the value 0

Step 3: If both the above steps are false then return the value 1

PHP Source Code

                                          <?php
function isLeap($year)
{
    if ($year % 4 == 0 && $year % 100 != 0) {
        return 1;
    } else if ($year % 400 != 0) {
        return 0;
    } else {
        return 1;
    }
}
$year = readline("Enter the year: ");
$check = isLeap($year);
if ($check == 1) {
    echo "$year is a leap year";
} else {
    echo "$year is not a leap year";
}
?>
                                      

OUTPUT

Example 1
Enter the year: 2012
2012 is a leap year

Example 2
Enter the year: 2014
2014 is not a leap year