What are some best practices for determining if a year is a leap year in PHP?
To determine if a year is a leap year in PHP, you can use the following logic: a year is a leap year if it is divisible by 4, unless it is divisible by 100 but not by 400. This means that a year like 2000 is a leap year because it is divisible by 400, while a year like 1900 is not a leap year because it is divisible by 100 but not by 400.
function isLeapYear($year) {
return (($year % 4 == 0) && ($year % 100 != 0)) || ($year % 400 == 0);
}
// Usage
$year = 2000;
if(isLeapYear($year)) {
echo $year . " is a leap year.";
} else {
echo $year . " is not a leap year.";
}
Keywords
Related Questions
- What additional information should be included when posting MySQL query code for troubleshooting on PHP forums?
- Are there specific PHP functions or libraries that can streamline the process of uploading and displaying images in a web application?
- What are some best practices for managing data within PHP session arrays to avoid data loss?