Checking For Leap Year Using PHP

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)));
}

Comments

  1. Keith

    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. Keith

    Whoops, small typo… if_numeric( $year ) should be is_numeric( $year )… :x


Be Heard!

Share your thoughts without being a jerk! And wrap your code in <code> tags, f00!

Name*:
Email*:
Website: