0
0
PhpProgramBeginner · 2 min read

PHP Program to Check Leap Year with Output and Explanation

You can check a leap year in PHP by using if (($year % 400 == 0) || (($year % 4 == 0) && ($year % 100 != 0))) to test the year and print the result.
📋

Examples

Input2000
Output2000 is a leap year.
Input1900
Output1900 is not a leap year.
Input2024
Output2024 is a leap year.
🧠

How to Think About It

To check if a year is leap, first see if it divides evenly by 400. If yes, it is leap. If not, check if it divides evenly by 4 but not by 100. If yes, it is leap. Otherwise, it is not leap.
📐

Algorithm

1
Get the input year.
2
Check if the year is divisible by 400; if yes, it is a leap year.
3
Else, check if the year is divisible by 4 and not divisible by 100; if yes, it is a leap year.
4
Otherwise, it is not a leap year.
5
Print the result.
💻

Code

php
<?php
$year = 2024;
if (($year % 400 == 0) || (($year % 4 == 0) && ($year % 100 != 0))) {
    echo "$year is a leap year.";
} else {
    echo "$year is not a leap year.";
}
?>
Output
2024 is a leap year.
🔍

Dry Run

Let's trace the year 1900 through the code to see if it is a leap year.

1

Input Year

year = 1900

2

Check divisibility by 400

1900 % 400 = 300 (not zero), so condition false

3

Check divisibility by 4 and not by 100

1900 % 4 = 0 (true), 1900 % 100 = 0 (true), so combined condition false

4

Result

1900 is not a leap year

StepConditionResult
Divisible by 400?1900 % 400 == 0False
Divisible by 4?1900 % 4 == 0True
Not divisible by 100?1900 % 100 != 0False
Leap Year?False || (True && False)False
💡

Why This Works

Step 1: Divisible by 400

If the year divides evenly by 400, it is always a leap year because it accounts for century years.

Step 2: Divisible by 4 but not 100

If the year divides evenly by 4 but not by 100, it is a leap year because normal leap years occur every 4 years except centuries.

Step 3: Otherwise not leap

If neither condition is met, the year is not a leap year.

🔄

Alternative Approaches

Using DateTime class
php
<?php
$year = 2024;
$date = DateTime::createFromFormat('Y-m-d', "$year-02-29");
if ($date && $date->format('m-d') === '02-29') {
    echo "$year is a leap year.";
} else {
    echo "$year is not a leap year.";
}
?>
This method uses PHP's DateTime to check if Feb 29 exists in the year, which is more readable but slightly slower.
Using a function
php
<?php
function isLeapYear(int $year): bool {
    return ($year % 400 == 0) || (($year % 4 == 0) && ($year % 100 != 0));
}
$year = 2024;
echo isLeapYear($year) ? "$year is a leap year." : "$year is not a leap year.";
?>
Encapsulating logic in a function improves reusability and clarity.

Complexity: O(1) time, O(1) space

Time Complexity

The program performs a fixed number of arithmetic and logical operations regardless of input size, so it runs in constant time O(1).

Space Complexity

The program uses a fixed amount of memory for variables and no extra data structures, so space complexity is O(1).

Which Approach is Fastest?

The direct arithmetic check is fastest and simplest; using DateTime is more readable but slower due to object creation.

ApproachTimeSpaceBest For
Arithmetic checkO(1)O(1)Fastest and simplest
DateTime classO(1)O(1)Readability and built-in validation
Function encapsulationO(1)O(1)Code reuse and clarity
💡
Remember that years divisible by 100 are not leap years unless divisible by 400.
⚠️
Beginners often forget to exclude years divisible by 100 but not by 400 as leap years.