Php Leap Year
Syntax
bool checkLeapYear(int $year)
Example
<?php
function checkLeapYear($year){
return (($year % 4 == 0) && ($year % 100 != 0)) || ($year % 400 == 0);
}
if(checkLeapYear(2024)){
echo "2024 is a Leap Year";
} else {
echo "2024 is not a Leap Year";
}
?>
Output
2024 is a Leap Year
Explanation
In the above code, we have created a function named checkLeapYear()
that takes a year as a parameter. Inside the function, we have used the formula to check if a year is a leap year or not.
(($year % 4 == 0) && ($year % 100 != 0)) || ($year % 400 == 0)
The first part of the formula checks if the year is divisible by 4 and not divisible by 100. If this is true, then the year is a leap year. The second part of the formula checks if the year is divisible by 400. If this is true, then the year is a leap year.
Use
This function is used to determine whether a given year is a leap year or not in PHP.
Important Points
- A leap year is a year that is divisible by 4, but not by 100, unless it is divisible by 400.
- The
checkLeapYear()
function returns a boolean value (true/false) based on whether a given year is a leap year or not. - This function can be used in various date and time based applications.
Summary
- In this tutorial, we learned how to create a function to check if a year is a leap year or not in PHP.
- The
checkLeapYear()
function returns a true or false value based on whether a given year is a leap year or not. - Leap year calculations are important in date and time applications and form an important part of calendar systems.