Gets the number of days in the month.
<?php
/**
* Returns the number of days in the requested month.
*
* @param int month as a number (1-12)
* @param int the year, leave empty for current
* @return int the number of days in the month
*/
function days_in_month($month, $year = null) {
$year = !empty($year) ? (int) $year : (int) date('Y');
$month = (int) $month;
if ($month < 1 || $month > 12) {
return 0;
} elseif ($month == 2) {
if ($year % 400 == 0 || ($year % 4 == 0 && $year % 100 != 0)) {
return 29;
}
}
$days_in_month = array(
31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
);
return $days_in_month[$month - 1];
}
// Usage
echo days_in_month("4", 2012); // 30