PHP Leap Year Program

Beginner
5 min read
Updated: Oct 2026
2 programs
Live preview

What Is a Leap Year?

A leap year has 366 days (February has 29). In the Gregorian calendar, leap years follow the 4 / 100 / 400 rule.

Remember
year is leap  ⇔
  (year % 4 === 0 AND year % 100 !== 0)
  OR
  (year % 400 === 0)

Examples:
  2024 → divisible by 4, not by 100     → leap
  1900 → divisible by 100, not by 400   → not leap
  2000 → divisible by 400               → leap
  2023 → not divisible by 4             → not leap
YearWhyLeap?
2024% 4 === 0, not a centuryYes
2000% 400 === 0Yes
1900century, not % 400No
2023not % 4No

How to Check a Leap Year in PHP

Four clear steps — then write it as one expression or nested ifs.

  1. If the year is not divisible by 4, it is not a leap year.
  2. If it is divisible by 4 but not by 100, it is a leap year.
  3. If it is divisible by 100, it must also be divisible by 400.
  4. Otherwise it is not a leap year.
MethodIdeaTimeBest for
Boolean expressionOne-line 4/100/400 predicateO(1)Interviews, short code
Nested ifSame rule, branched for reading aloudO(1)Explaining each condition

Pseudocode

Pseudocode
function IsLeapYear(year):
    if year mod 400 == 0:
        return true
    if year mod 100 == 0:
        return false
    if year mod 4 == 0:
        return true
    return false

Cheat Sheet

Keep these facts handy while writing the program.

TopicDetail
Rule(%4 && !%100) || %400
February29 days in a leap year
Century leapOnly if divisible by 400
Classic yes2024, 2000
Classic no1900, 2023
Strict comparePrefer === / !== in PHP

Live Preview

Enter any year to see each divisibility check and the final verdict.

Whole years from 1 to 9999. Tap a chip or type a year.

Result
year = 2024
2024 % 4 = 0
2024 % 100 = 24 (not a century year)
Verdict: LEAP YEAR

Worked Walkthrough — year = 2000 and 1900

Century years look similar until you apply the % 400 test.

Calculation
year = 2000
  2000 % 400 = 0  → leap  ✓

year = 1900
  1900 % 400 = 300 ≠ 0
  1900 % 100 = 0      → century, not leap  ✗

year = 2024
  2024 % 4 = 0
  2024 % 100 ≠ 0      → leap  ✓

PHP Programs

Start with a one-line boolean expression. Use nested ifs when you want to explain each rule out loud.

Example 1 — Boolean Expression

Encode the Gregorian rule in a single return statement.

PHP
<?php
function isLeapYear(int $year): bool {
    return ($year % 4 === 0 && $year % 100 !== 0) || ($year % 400 === 0);
}

$year = 2024;
if (isLeapYear($year)) {
    echo $year . " is a leap year\n";
} else {
    echo $year . " is not a leap year\n";
}
?>

How It Works

  1. $year % 4 === 0 is the basic every-4-years test.
  2. $year % 100 !== 0 excludes most century years.
  3. $year % 400 === 0 brings century leaps like 2000 back in.
  4. The || joins both paths into one boolean result.
  5. For 2024: divisible by 4, not by 100 → leap.

Example 2 — Nested If

Same logic, written as branches so each rule is easy to narrate in an interview.

PHP
<?php
function isLeapYear(int $year): bool {
    if ($year % 400 === 0) {
        return true;
    }
    if ($year % 100 === 0) {
        return false;
    }
    if ($year % 4 === 0) {
        return true;
    }
    return false;
}

echo isLeapYear(2000) ? "true\n" : "false\n";
echo isLeapYear(1900) ? "true\n" : "false\n";
echo isLeapYear(2024) ? "true\n" : "false\n";
?>

How It Works

  1. Check % 400 first — strongest leap rule for centuries.
  2. Then reject remaining century years with % 100 === 0.
  3. Then accept ordinary leap years with % 4 === 0.
  4. Anything left returns false.
  5. Same answers as Example 1 — pick the style that is clearer to explain.

Edge Cases & Pitfalls

Check these before calling the solution done.

Centuries

Not all % 4 years leap

1900 fails; 2000 passes because of % 400.

Only % 4

Incomplete rule

Interviewers often probe century years on purpose.

Order

Test 400 before 100

In nested form, checking % 100 first without % 400 breaks 2000.

Types

Use whole years

Type-hint int (or validate) before applying %.

Time and Space Complexity

ProgramTimeExtra space
Boolean expressionO(1)O(1)
Nested ifO(1)O(1)

A single-year check is a few modulus operations. Scanning a year range is O(range size).

Key Takeaways

  • Rule: (%4 && !%100) || %400.
  • Century trap: 1900 no, 2000 yes.
  • Two styles: one expression or nested ifs.
  • Test set: 2024 yes; 2023 no; 2000 yes; 1900 no.

One line: leap if divisible by 4 but not 100, or divisible by 400.

Frequently Asked Questions

In the Gregorian calendar: leap if (year % 4 === 0 && year % 100 !== 0) || (year % 400 === 0).
Yes. It is divisible by 400, so the century exception does not apply.
No. It is divisible by 100 but not by 400.
No. Century years need the extra % 400 test — that is why 1900 fails and 2000 passes.
Both are correct. Use the one-liner for short code; use nested ifs when you want to explain each rule aloud.

Next: Magic Number

Check whether a number reduces to 1 via digit sums.

Magic number tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

11 people found this page helpful