Perl has a module Date::Calc that does this type of calculation.
$days = Days_in_Month($year,$month);
This function returns the number of days in the given month $month'' of the given year$year''.
The year must always be supplied, even though it is only needed when the month is February, in order to determine wether it is a leap year or not.
I.e., Days_in_Month(1998,1)'' returns31'', Days_in_Month(1998,2)'' returns28'', Days_in_Month(2000,2)'' returns29'', Days_in_Month(1998,3)'' returns31'', and so on.