PHP cal_days_in_month() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Calendar

Definition

In PHP, cal_days_in_month() returns the number of days in a given month of a given year for the calendar you choose. For example, it tells you that February 2024 has 29 days.

This is handy for building calendars, billing cycles and date pickers, because you never have to write your own leap year rules. You pass the calendar type (usually CAL_GREGORIAN), the month number and the year, and PHP does the counting.

Syntax

Call cal_days_in_month() with the values you need. Start with this line.

PHP
$days = cal_days_in_month(CAL_GREGORIAN, 2, 2024);
CallWhat it does
cal_days_in_month(CAL_GREGORIAN, 1, 2024)Returns 31 because January always has 31 days.
cal_days_in_month(CAL_GREGORIAN, 2, 2024)Returns 29 because 2024 is a leap year.
cal_days_in_month(CAL_JULIAN, 2, 1900)Returns 29 because 1900 is a leap year in the Julian calendar.

Start with CAL_GREGORIAN, which is the calendar most people use today. The other calendar constants are CAL_JULIAN, CAL_JEWISH and CAL_FRENCH. If you only work with Gregorian dates, you can also use date('t') on a timestamp.

Library declarations
PHP
cal_days_in_month(int $calendar, int $month, int $year): int

Parameters

ParameterDescription
calendarRequired. The calendar to use. Pass CAL_GREGORIAN (0), CAL_JULIAN (1), CAL_JEWISH (2) or CAL_FRENCH (3). Any other value throws a ValueError.
monthRequired. The month number in that calendar. For the Gregorian calendar this is 1 (January) to 12 (December).
yearRequired. The year in that calendar, such as 2024. Year 0 does not exist in the Gregorian and Julian calendars.

Return Value

Return ValueDescription
intThe number of days in the given month, for example 28, 29, 30 or 31 in the Gregorian calendar. An invalid month or year throws a ValueError with the message Invalid date.

PHP Examples

Example 1 — Days in Different Months

Ask for the length of a few Gregorian months, including February in a leap year and in a normal year.

PHP
<?php
echo cal_days_in_month(CAL_GREGORIAN, 1, 2024), PHP_EOL;
echo cal_days_in_month(CAL_GREGORIAN, 2, 2023), PHP_EOL;
echo cal_days_in_month(CAL_GREGORIAN, 2, 2024), PHP_EOL;
echo cal_days_in_month(CAL_GREGORIAN, 4, 2024), PHP_EOL;

How It Works

  1. CAL_GREGORIAN tells PHP to use the normal Gregorian calendar.
  2. January has 31 days, and April has 30 days.
  3. February 2023 has 28 days because 2023 is not a leap year.
  4. February 2024 has 29 days because 2024 is a leap year.

Example 2 — Leap Year Rules and Invalid Months

The year 2000 is a leap year but 1900 is not. A different calendar can also give a different answer, and a bad month throws an error.

PHP
<?php
echo cal_days_in_month(CAL_GREGORIAN, 2, 2000), PHP_EOL;
echo cal_days_in_month(CAL_GREGORIAN, 2, 1900), PHP_EOL;
echo cal_days_in_month(CAL_JULIAN, 2, 1900), PHP_EOL;

try {
    cal_days_in_month(CAL_GREGORIAN, 13, 2024);
} catch (ValueError $e) {
    echo 'Caught: ', $e->getMessage(), PHP_EOL;
}

How It Works

  1. The year 2000 is divisible by 400, so it is a leap year and February has 29 days.
  2. The year 1900 is divisible by 100 but not by 400, so the Gregorian calendar gives 28 days.
  3. The Julian calendar treats every fourth year as a leap year, so February 1900 has 29 days there.
  4. Month 13 does not exist, so PHP 8 throws a ValueError that we catch and print.

Example 3 — Count the Days in a Year

Loop through all twelve months and add up the days to find out how long a year is.

PHP
<?php
function daysInYear(int $year): int
{
    $total = 0;

    for ($month = 1; $month <= 12; $month++) {
        $total += cal_days_in_month(CAL_GREGORIAN, $month, $year);
    }

    return $total;
}

echo '2024: ', daysInYear(2024), PHP_EOL;
echo '2023: ', daysInYear(2023), PHP_EOL;

How It Works

  1. The function starts a running total at zero.
  2. The loop visits months 1 to 12 and adds the number of days in each one.
  3. 2024 is a leap year, so February adds 29 days and the total becomes 366.
  4. 2023 is a normal year, so the total is 365.

Frequently Asked Questions

It returns how many days a month has in a chosen calendar and year. For the Gregorian calendar it returns 28, 29, 30 or 31, and it already knows the leap year rules.
The calendar extension is not enabled on your server. Enable extension=calendar in php.ini, or install the php-calendar package on Linux, then restart PHP or your web server.
Ask for February. If cal_days_in_month(CAL_GREGORIAN, 2, $year) returns 29, the year is a leap year. If it returns 28, it is a normal year.
date('t') needs a timestamp and works only with the Gregorian calendar. cal_days_in_month() takes plain month and year numbers and can also handle Julian, Jewish and French calendars.
In PHP 8 it throws a ValueError with the message Invalid date. Use a try and catch block, or check that the month is between 1 and 12 before calling the function.

Next: cal_from_jd()

cal_from_jd() converts a Julian Day number into calendar date details in one array.

cal_from_jd() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful