Calculate Days In A Month - PHP

Total Views : 59
Zoom In Zoom Out Read Later Print

For this, we will create a simple function to return days in a given month.

I should mention that PHP also offers a cal_days_in_month function for PHP 4.0.7 and higher, but I prefer using the below function because it is guaranteed to work on all PHP version without any issues because it is based on logic.

get_days_in_month($month, $year)

function get_days_in_month($month, $year){
   return $month == 2 ? ($year % 4 ? 28 : ($year % 100 ? 29 : ($year %400 ? 28 : 29))) : (($month - 1) % 7 % 2 ? 30 : 31);
}

This function provide days in month for all years including leap years.

See More

Latest Photos