PHP leap year calculation
A simple little function to calculate whether a specified year is a leap year or not:
function isleapyear($year = '') {
if (empty($year)) {
$year = date('Y');
}
$year = (int) $year;
if ($year % 4 == 0) {
if ($year % 100 == 0) {
return ($year % 400 == 0);
} else {
return true;
}
} else {
return false;
}
}
The algorithm applied above, translated to everyday language, is something like this:
If the year is divisible by 100, then it's not, unless the year is divisible by 400, when it is. Otherwise, if the year is divisible by 4, then it is. Otherwise, it's not.
Did you get all that? I know, it's probably easier to understand by looking at the code.
Due to the large volume of spam, comments are disabled. If you have anything relevant to say, you can leave a , or contact me directly.