Find The Number Of Days For A Given Month With PHP

There are two ways to find out the number of days for a given month. The first is to use the date() function in conjunction with the mktime() function to create a date and format this value as the number of days in a given month.

$monthDays = date("t",mktime(0, 0, 0, 12, 1, 2008));

The second way is to use the function cal_days_in_month(). This function takes three parameters.

  • Canelday: There are a number of different forms of calendar to chose from. The one most English speaking people are interested in is the CAL_GREGORIAN calendar. Take a look at a full list of calendar constants.
  • Month: An integer representing the month (1 - 12).
  • Year: An integer representing the year.

To find out the number of days in the month of December of 2008 you could use the following.

$monthDays = cal_days_in_month(CAL_GREGORIAN, 12, 2008);

 

Add new comment

The content of this field is kept private and will not be shown publicly.
CAPTCHA
7 + 3 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.