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.

About the author

Ivan's mugshotI'm Ivan Lutrov and I'm the owner of Lutrov Interactive. I have 25 years of experience producing interactive work and I create cost effective business websites that are simple, engaging and easy to use. I preach what I practice, and I say what I really think, even if it's sometimes not what you expect to hear. Subscribe to the Lutrov Interactive feed via RSS and follow me on Twitter.