PHP Program to Check a Given Year is a Leap Year or Not
Last Updated :
17 Jan, 2024
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;
}
}
$year = 2024;
if (isLeapYear( $year )) {
echo "Leap Year" ;
} else {
echo "Not a 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" ));
}
$year = 2024;
if (isLeapYear( $year )) {
echo "Leap Year" ;
} else {
echo "Not a Leap Year" ;
}
?>
|
Share your thoughts in the comments
Please Login to comment...