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
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
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