PHP: is it a leap year?

I’ve written about date manipulation in PHP before. There are some truly horrible hacks out there. Today I needed to detect whether a given year was a leap year, in order to write future-proof code for a calendar. PHP has got to have a way of doing this. I Googled and found this. And this, which is almost as bad. Urgh. Sorry, but that is one of those occasions when the coder should have asked him/herself if there wasn’t a better way, and at least considered looking at the manual for the date function. There is of course a quick and simple way. Here it is.

So I wrote one line of code:
$maxfebday = date('L', strtotime("$currentyear-01-01")) ? 29 : 28;

This entry was posted in PHP. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>