Checking For Leap Year Using PHP

By  on  

One part of programming that seems pretty static is dealing with dates. The calendar is a set system of rules that doesn't look to change. The only part of the calendar that can be variable is a leap year, which changes every four years (obviously).

Using pure PHP ternary logic, much like the PHP Function - Calculating Days In A Month, I posted a few weeks back, you can check to see if a year is a leap year.

The Code

function is_leap_year($year) {
	return ((($year % 4) == 0) && ((($year % 100) != 0) || (($year % 400) == 0)));
}

Recent Features

  • By
    An Interview with Eric Meyer

    Your early CSS books were instrumental in pushing my love for front end technologies. What was it about CSS that you fell in love with and drove you to write about it? At first blush, it was the simplicity of it as compared to the table-and-spacer...

  • By
    How to Create a RetroPie on Raspberry Pi – Graphical Guide

    Today we get to play amazing games on our super powered game consoles, PCs, VR headsets, and even mobile devices.  While I enjoy playing new games these days, I do long for the retro gaming systems I had when I was a kid: the original Nintendo...

Incredible Demos

  • By
    Save Web Form Content Using Control + S

    We've all used word processing applications like Microsoft Word and if there's one thing they've taught you it's that you need to save every few seconds in anticipation of the inevitable crash. WordPress has mimicked this functionality within their WYSIWYG editor and I use it...

  • By
    MooTools 1.2 Tooltips: Customize Your Tips

    I've never met a person that is "ehhhh" about XHTML/javascript tooltips; people seem to love them or hate them. I'm on the love side of things. Tooltips give you a bit more information about something than just the element itself (usually...

Discussion

  1. Placing a function call as an argument default will result in a fatal error. Also, give this version a shot:

    function is_leap_year( $year = NULL )
    {
        if_numeric( $year ) || $year = date( 'Y' );
        return checkdate( 2, 29, ( int ) $year );
    }
  2. Whoops, small typo… if_numeric( $year ) should be is_numeric( $year )… :x

  3. Tamlyn

    Better yet, use date('L') which returns 1 if it’s a leap year, 0 if it isn’t.

  4. I do agree with tamlyn, why you dont use date function??
    It can makes a load fasting right?

  5. Travis

    date(‘L’) is way better because leap year is not every 4 years.

  6. Date("L") only tell you in a given year, default is the year today.
    If you need to know whether previous or next year is a leap, you must reset the date.
    It could affect the system.

    • Indra, you can always pass the timestamp as second parameter to date function:

      echo date('L', mktime(1, 1, 1, 1, 1, 2005));
      
  7. I think this is a better example relating to the function. Also provided example for anyone looking for true/false as I was in this instance.

    /* for true or false */
    function is_leap_year($year)
    {
    	return ( date ('L', mktime(1,1,1,1,1, $year) ) === 1 ) ? true : false;
    }
    /*for 0 or 1 Whether it's a leap year: 1 if it is a leap year, 0 otherwise. */
    function is_leap_year($year)
    {
    	return date ('L', mktime(1,1,1,1,1, $year) );		
    }
    
  8. Habibur Rahaman
    <?php
    $day = "";
    for($i=0; $i<4; $i++)
    {
        $day =  date("d", mktime(0, 0, 0, 2, 29, date("Y")+$i));
        if($day == 29)
        {
            $year = date("Y")+$i;
            break;
        }
    }
    echo "The next leap year is 29th February $year";    
    ?>
    
    
  9. Milan Dvořák

    code of Habibur Rahaman will not work for example for the year 1897, because 1900 is not leap year.

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!