PHP provides functions to get the number of days in a month for a specified year and calendar. We can use date(‘t’) or cal_days_in_month().
date('t')
‘t‘ in date(‘t’) is date format option which gives number of days in the given month.
Example
1. date(‘t’) returns number of days in current month
1 2 3 4 | $days = date('t'); echo $days; // Output: number of days in current month |
2. Returns number of days in a given month. We can given any date of that month.
1 2 3 | echo date('t',strtotime('2014/10/01')); // Output 31 |
cal_days_in_month
The cal_days_in_month()
function returns the number of days in a month for a specified year and calendar. It is supported in PHP 4 >= 4.1.0, PHP 5.
Syntax
1 | int cal_days_in_month ( int $calendar , int $month , int $year ) |
Parameters
1. calendar: Required, Calendar to use for calculation
2. month: Required, Month in the selected calendar
3. year: Required, Year in the selected calendar
Example:
1 2 3 4 | <?php $num = cal_days_in_month(CAL_GREGORIAN, 10, 2014); // 31 echo "There was $num days in October 2014"; ?> |