PHP program to check if a year is leap year or not

To check if a year is leap year or not in PHP, we need to apply the leap year rules. A year is a leap year if it is divisible by 400, or divisible by 4 but not by 100.

Leap Year Rules

The rules for determining a leap year are ?

  • If a year is divisible by 400, it is a leap year
  • If a year is divisible by 100 (but not 400), it is not a leap year
  • If a year is divisible by 4 (but not 100), it is a leap year
  • Otherwise, it is not a leap year

Example

<?php
    function year_check($my_year){
        if ($my_year % 400 == 0)
            print("$my_year is a leap year");
        else if ($my_year % 100 == 0)
            print("$my_year is not a leap year");
        else if ($my_year % 4 == 0)
            print("$my_year is a leap year");
        else
            print("$my_year is not a leap year");
    }
    
    $my_year = 1900;
    year_check($my_year);
    
    echo "
"; // Test with a leap year $my_year = 2000; year_check($my_year); ?>

Output

1900 is not a leap year
2000 is a leap year

Alternative Method Using Built-in Function

PHP also provides a built-in function to check leap years using the checkdate() function ?

<?php
    function isLeapYear($year) {
        // February 29th exists only in leap years
        return checkdate(2, 29, $year);
    }
    
    $year = 2024;
    if (isLeapYear($year)) {
        echo "$year is a leap year";
    } else {
        echo "$year is not a leap year";
    }
?>
2024 is a leap year

Conclusion

A leap year occurs every 4 years with exceptions for century years. Use the modulo operator to check divisibility rules, or leverage PHP's checkdate() function for a simpler approach.

Updated on: 2026-03-15T09:05:25+05:30

14K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements