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)) then`

`isLeapYear = True`

`else`

`isLeapYear = False`

`end if`

`end 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 someMonth`

`case 1, 3, 5, 7, 8, 10, 12`

`MonthDays = 31`

`case 4, 6, 9, 11`

`MonthDays = 30`

`case 2`

`if isLeapYear(someYear) then`

`MonthDays = 29`

`else`

`MonthDays = 28`

`end if`

`end select`

`end 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