This year (2008) is a leap year, and this past week contained February 29. It seems only fitting that we talk about calculating leap years and what we can do with that information.
A leap year is always divisible by four, but not by one hundred unless it is also divisible by four hundred.
ASP
function isLeapYear(someYear)if someYear Mod 4 = 0 and (someYear Mod 100 <> 0 or (someYear Mod 100 = 0 and someYear Mod 400 = 0)) thenisLeapYear = TrueelseisLeapYear = Falseend ifend function
PHP
function isLeapYear($someYear){return date("L", strtotime($someYear . "-01-01"));}
Now let's write a function to build on this which returns the number of days in a given month.
ASP
function MonthDays(someMonth, someYear)select case someMonthcase 1, 3, 5, 7, 8, 10, 12MonthDays = 31case 4, 6, 9, 11MonthDays = 30case 2if isLeapYear(someYear) thenMonthDays = 29elseMonthDays = 28end ifend selectend function
PHP
function MonthDays($someMonth, $someYear){return date("t", strtotime($someYear . "-" . $someMonth . "-01"));}
UPDATE: Thanks to Jim Mayes for showing me more elegant PHP solutions.

No comments:
Post a Comment