how to determin if a specific year is a leap year in PHP
I don’t know if I’m just on a refactoring kick lately or what… Ok, how to determine if a year is a leap year. And the related question, how many days are in a given month. If you’re working with dates, there’s a good chance you will need a way to do both at some point.
Stumbled on a post over at the reusable code blog that makes a valiant attempt to provide a solution for those two needs. Again good solid code that definitely gets the right results… but something that can be accomplished in a much more efficient way with just a few native PHP functions. So let’s jump into it!
How to check if a specific year is a leap year in PHP? No need to start dividing by 4 except if when divisible by 100 unless simultaneously divisible by 400. Yeah, I can sense you scratching your head and going back to re-read that last sentence again. And that’s exactly what I’m talking about. Stop it now! Restrain that little voice in your head that’s saying, “oh a problem, I bet I can find a really slick way to solve it.” You don’t need to figure out how to build that equation. All you need is PHP’s date() function.
echo date("L");
That will print 1 if the current year is a leap year, 0 if it is not, as anti-climatic as that might be. Now let’s build that into a truly reusable function by adding in some strtotime() magic!
function is_leapyear($year){ return date("L", strtotime($year . "-01-01")); }
Which you then use like this:
echo ( is_leapyear(2007) )? "Leap Year" : "Not a Leap Year";
3 lines of code versus 11!
Next up, finding how many days are in a given month? The afore mentioned examples build a function which uses a switch statement to hard code the months with 30 days, the months with 31 days and then makes use of the the leap year detection function to decide if February has 28 or 29 days for the year in question. Wheeew, another long description for a solution which brings the total lines of code to accurately print out the number of days in this past February to 42! We’re gonna slim that down quite a bit!
Once again, PHP’s date() function…
echo date("t");
Yup, that will print the number of days for the current month. Let’s add in strtotime() for a nice tight reusable function.
function days_in_month($year, $month){ return date("t", strtotime($year . "-" . $month . "-01")); }
And equally simple usage:
echo days_in_month(2007, 02);
And what’s more, this doesn’t require the previous is_leapyear() to get the job done.
Again, not faulting anybody for trying to solve a problem. In fact, as I’ve said in previous posts, these over engineered solutions often display a great deal of ingenuity and problem solving ability. And of course, I can’t speak for the ASP code posted there. But, not every wheel needs to be invented, or re-invented for that matter. But when I see something like this I’m gonna post about it so hopefully others will find these posts as well as the other code that has been spread all of the internet. Seriously, google for “php is leap year“. People have created classes to do something that a built in PHP function can provide.
Scott
Jim, I just discovered your comments on Snippr and followed your profile back to your blog. Thank you for showing me a more elegant solution! I started out as an ASP programmer, where we don’t have such luxuries as PHP’s date() function. I still consider myself a PHP novice, but I am in the slow process of switching over.