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.
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.
©2004 - 2010 scriptplayground | Privacy Policy | Legal
Validate Site: XHTML CSS | Designed by: Matthew Keefe of mkeefeDESIGN