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)));
}
![Write Better JavaScript with Promises]()
You've probably heard the talk around the water cooler about how promises are the future. All of the cool kids are using them, but you don't see what makes them so special. Can't you just use a callback? What's the big deal? In this article, we'll...
![Introducing MooTools Templated]()
One major problem with creating UI components with the MooTools JavaScript framework is that there isn't a great way of allowing customization of template and ease of node creation. As of today, there are two ways of creating:
new Element Madness
The first way to create UI-driven...
![Create Your Own Dijit CSS Theme with LESS CSS]()
The Dojo Toolkit seems to just get better and better. One of the new additions in Dojo 1.6 was the use of LESS CSS to create Dijit themes. The move to using LESS is a brilliant one because it makes creating your own Dijit theme...
![MooTools ASCII Art]()
I didn't realize that I truly was a nerd until I could admit to myself that ASCII art was better than the pieces Picasso, Monet, or Van Gogh could create. ASCII art is unmatched in its beauty, simplicity, and ... OK, well, I'm being ridiculous; ASCII...
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 ); }Whoops, small typo… if_numeric( $year ) should be is_numeric( $year )… :x
Better yet, use
date('L')which returns1if it’s a leap year,0if it isn’t.I do agree with tamlyn, why you dont use date function??
It can makes a load fasting right?
date(‘L’) is way better because leap year is not every 4 years.
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));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) ); }<?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"; ?>code of Habibur Rahaman will not work for example for the year 1897, because 1900 is not leap year.