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)));
}
How is it possible that JavaScript-based animation has secretly always been as fast — or faster — than CSS transitions? And, how is it possible that Adobe and Google consistently release media-rich mobile sites that rival the performance of native apps?
This article serves as a point-by-point...
One of my favorite social APIs was the Open Graph API adopted by Facebook. Adding just a few META tags to each page allowed links to my article to be styled and presented the way I wanted them to, giving me a bit of control...
A few of my customer have asked for me to create a subtle but dynamic (...I know...) way for them to advertise different specials on their website. Not something that would display on every page, but periodically or only the homepage. Using a trick...
When I first wrote MooTools ElementFilter, I didn't think much of it. Fast forward eight months later and I've realized I've used the plugin a billion times. Hell, even one of the "big 3" search engines is using it for their maps application.
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.