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)));
}
![Convert XML to JSON with JavaScript]()
If you follow me on Twitter, you know that I've been working on a super top secret mobile application using Appcelerator Titanium. The experience has been great: using JavaScript to create easy to write, easy to test, native mobile apps has been fun. My...
![5 Ways that CSS and JavaScript Interact That You May Not Know About]()
CSS and JavaScript: the lines seemingly get blurred by each browser release. They have always done a very different job but in the end they are both front-end technologies so they need do need to work closely. We have our .js files and our .css, but...
![CSS Columns]()
One major gripe that we've always had about CSS is that creating layouts seems to be more difficult than it should be. We have, of course, adapted and mastered the techniques for creating layouts, but there's no shaking the feeling that there should be a...
![LightFace: Facebook Lightbox for MooTools]()
One of the web components I've always loved has been Facebook's modal dialog. This "lightbox" isn't like others: no dark overlay, no obnoxious animating to size, and it doesn't try to do "too much." With Facebook's dialog in mind, I've created LightFace: a Facebook lightbox...
Placing a function call as an argument default will result in a fatal error. Also, give this version a shot:
Whoops, small typo… if_numeric( $year ) should be is_numeric( $year )… :x
Better yet, use
date('L')
which returns1
if it’s a leap year,0
if 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:
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.
code of Habibur Rahaman will not work for example for the year 1897, because 1900 is not leap year.