When A Leap Year Really Isn't

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

So that:

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

Add new comment

Markdown

  • Allowed HTML tags: <a href hreflang> <em> <strong> <cite> <blockquote cite> <code> <ul type> <ol start type> <li> <dl> <dt> <dd>
  • Missing filter. All text is removed
CAPTCHA This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.