W3docs

cal_days_in_month()

Are you looking for a simple and effective way to calculate the number of days in a month using PHP? If so, you have come to the right place. In this article,

The PHP cal_days_in_month() function returns the number of days in a month for a given calendar, month, and year. Because it understands leap years and different calendar systems, it is more reliable than hard-coding day counts or doing the leap-year math by hand. This page covers its signature, working examples (including leap years and the Julian calendar), common gotchas, and the everyday alternative when the Calendar extension is unavailable.

Syntax

cal_days_in_month(int $calendar, int $month, int $year): int
ParameterDescription
$calendarCalendar to use: CAL_GREGORIAN, CAL_JULIAN, CAL_JEWISH, or CAL_FRENCH.
$monthMonth number in the chosen calendar (112 for Gregorian/Julian).
$yearThe year, as an integer.

It returns the number of days as an int. cal_days_in_month() is part of the Calendar extension, which is bundled with PHP but must be enabled (it is by default on most builds). If the extension is missing, the function does not exist; see Without the Calendar extension below.

Basic usage

Pass the calendar constant, the month, and the year:

php— editable, runs on the server

This prints There are 31 days in January 2022.

Handling leap years

The function automatically accounts for leap years, so February returns 28 or 29 without any extra logic on your part:

<?php
echo cal_days_in_month(CAL_GREGORIAN, 2, 2020); // 29 — 2020 is a leap year
echo "\n";
echo cal_days_in_month(CAL_GREGORIAN, 2, 2023); // 28 — 2023 is not
?>

Listing every month in a year

A common task is building a calendar or a date dropdown. Loop over the 12 months:

<?php
$year = 2024;
$names = ['January', 'February', 'March', 'April', 'May', 'June',
          'July', 'August', 'September', 'October', 'November', 'December'];

for ($month = 1; $month <= 12; $month++) {
    $days = cal_days_in_month(CAL_GREGORIAN, $month, $year);
    echo "{$names[$month - 1]} $year: $days days\n";
}
?>

For 2024 (a leap year) this prints February 2024: 29 days, while every other month shows its usual count.

Using a different calendar

Switching the first argument changes the calendar system. The Julian calendar, for example, treats 1900 as a leap year where the Gregorian calendar does not:

<?php
echo cal_days_in_month(CAL_JULIAN, 2, 1900);    // 29
echo "\n";
echo cal_days_in_month(CAL_GREGORIAN, 2, 1900); // 28
?>

Common gotchas

  • Invalid month or year: passing a month outside the valid range (for example 13 or 0) raises a warning and returns false, not a day count. Validate input before calling.
  • Type juggling: false is loosely equal to 0. Use a strict check (if ($days === false)) rather than if (!$days) when handling errors.
  • Extension dependency: the function only exists when the Calendar extension is loaded. Guard with function_exists('cal_days_in_month') in portable code.

Without the Calendar extension

If the Calendar extension is not available, the DateTime class (always available) gives the same Gregorian result via the t format character, which is the number of days in the month:

<?php
$date = new DateTime('2024-02-01');
echo $date->format('t'); // 29
?>

This is the most portable option for the Gregorian calendar and needs no extra extension.

Conclusion

cal_days_in_month() returns the day count for a month in a given calendar and year, handling leap years and multiple calendar systems for you. Reach for it when you work with the Calendar extension or need a non-Gregorian calendar; otherwise DateTime::format('t') is a dependency-free alternative.

To keep working with dates in PHP, see PHP Date and Time and the checkdate() function for validating a Gregorian date.

Diagram

graph LR
A[Input Calendar Type, Month, and Year] -- cal_days_in_month --> B[Output Number of Days]

Practice

Practice
What does the cal_days_in_month() function in PHP do?
What does the cal_days_in_month() function in PHP do?
Was this page helpful?