In the previous blog entry, 3. Are You Odd?, I referred to determining a leap year still being a manual programming process in many cases. There are certainly umpteen library functions out there to do it, but it's simple to do and a nice programming exercise for function writing.
How does one determine if a given year is a leap year? It's a common belief that if the year is evenly divisible by 4, that it is a leap year. That is true most of the time, but not always.
The rule for determining a leap year is this:
A Year is a Leap Year ...
- Rule 1 - it is evenly divisible by 4
- Rule 2 - Rule 1 is true only if the year is not evenly divisible by 100
- Rule 3 - Rule 2 is true only if the year is not evenly divisible by 400
2004 is a leap year (Rule 1)
1900 would seem to be a leap year (Rule 1), but is not a leap year (Rule 2)
2000 would not seem to be a leap year (Rule 2), but is a leap year (Rule 3)
With the rules laid out, here then is a PHP function to test for a leap year