Subscribe to Tutorial Feed

Flash and PHP Bible

The Flash and PHP Bible has been released! The book can be found on Amazon or wherever fine books are sold in your area.

The Flash and PHP Bible has a forum for quick support.

Scriptplayground » tutorials » php » Detecting Leap Year Using PHP

Detecting Leap Year Using PHP

A quick snippet of code to detect if a certain date is a leap year or not.

PHP has a number of "hidden" abilities contained within its functions. One of those is the ability to detect if its a leap year using the date() function.

Lets look at the code required to accomplish this.

print date('L');

As you can see, the code is minimal and straightforward. In the above example you are basically asking if the current year is in fact a leap year. Lets expand on that example and wrap it in a function to quickly validate any date.

function isLeapYear($year=null)
{
	if(is_null($year))
	{
		return date('L');
	}
	else
	{
		return date('L', strtotime($year . "-01-01"));
	}
}

Now that the function is complete, lets look at an example usage.

if(isLeapYear(2008))
{
	print "2008 is a leap year";
}
if(isLeapYear(2009))
{
	print "2009 is NOT a leap year";
}

I hope you found this little tutorial useful for the next time you need validate if a date is a leap year.

Follow Scriptplayground on Twitter (@scriptplay)

Comments: Detecting Leap Year Using PHP

No Comments have been made. Wanna be the first?
Add a comment
The ability to add new comments has been disabled in this archived version.

Loading
Main | Tutorials | Articles