Open In App

PHP Program to Check a Given Year is a Leap Year or Not

Last Updated : 17 Jan, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

A leap year is a year that is divisible by 4, except for years that are divisible by 100. However, years divisible by 400 are also considered leap years. In this article, we will explore different approaches to create a PHP program that determines whether a given year is a leap year or not.

Using Conditional Statements

The basic method involves using conditional statements to check the leap year conditions.

PHP




<?php
  
function isLeapYear($year) {
    if (($year % 4 == 0 && $year % 100 != 0) 
        || ($year % 400 == 0)) {
        return true;
    } else {
        return false;
    }
}
  
// Driver code
$year = 2024;
  
if (isLeapYear($year)) {
    echo "Leap Year";
} else {
    echo "Not a Leap Year";
}
  
?>


Output

Leap Year

Using date Function

The date function can be utilized to determine if a given year is a leap year by checking if the 29th of February exists.

PHP




<?php
  
function isLeapYear($year) {
    return date('L', strtotime("$year-01-01"));
}
  
// Driver code
$year = 2024;
  
if (isLeapYear($year)) {
    echo "Leap Year";
} else {
    echo "Not a Leap Year";
}
  
?>


Output

Leap Year


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads