Content · Programming · PHP

PHP cal_days_in_month() Function: Usage and Examples

Learn how PHP cal_days_in_month works with practical examples, important behavior, relevant alternatives and common mistakes.

2 views Published Oct 16, 2026 Updated Oct 16, 2026

Returns the number of days in a calendar month.

What Is cal_days_in_month() in PHP?

Returns the number of days in a calendar month.

Its syntax is:

cal_days_in_month(int $calendar, int $month, int $year): int

Parameters:

  • $calendar — Accepted as int; required argument.
  • $month — Accepted as int; required argument.
  • $year — Accepted as int; required argument.

Return type: int. Use the appropriate calendar constant. February depends on leap years.

Availability: Requires the calendar extension.

Example 1: Basic Usage

<?php

echo cal_days_in_month(CAL_GREGORIAN, 2, 2024);

Output:

29

Use the appropriate calendar constant. February depends on leap years.

Example 2: A Different Input

<?php

echo cal_days_in_month(CAL_GREGORIAN, 4, 2025);

Output:

30

Use the appropriate calendar constant. February depends on leap years.

Practical Example: Applying It in a Program

<?php

$months = [2, 4, 12]; foreach ($months as $month) { echo "$month: " . cal_days_in_month(CAL_GREGORIAN, $month, 2025) . PHP_EOL; }

Output:

2: 28
4: 30
12: 31

Use the appropriate calendar constant. February depends on leap years.

Important Behavior and Edge Cases

  • Key behavior: Use the appropriate calendar constant. February depends on leap years.
  • Environment: Requires the calendar extension.

Comparison With Related Functions

FunctionPurpose
cal_days_in_month()Returns the number of days in a calendar month.
date("t")date("t") returns the days in a month of a PHP date; cal_days_in_month() supports a specified calendar system.

Common Beginner Mistakes

MistakeCorrect Approach
Ignoring this behavior: Use the appropriate calendar constantUse the appropriate calendar constant. February depends on leap years.

Key Takeaways

  • Returns the number of days in a calendar month.
  • Use the appropriate calendar constant. February depends on leap years.
  • Requires the calendar extension.

Summary: Use the appropriate calendar constant. February depends on leap years.

Share this resourceSend this page to someone who may find it useful.