Returns calendar names and month information for supported calendars.
What Is cal_info() in PHP?
Returns calendar names and month information for supported calendars.
Its syntax is:
cal_info(int $calendar = -1): array
Parameters:
$calendar— Accepted as int; optional default -1.
Return type: array. The result structure differs when requesting all calendars rather than one.
Availability: Requires the calendar extension.
Example 1: Basic Usage
<?php
print_r(cal_info(CAL_GREGORIAN)["months"]);
Output:
Array
(
[1] => January
[2] => February
[3] => March
[4] => April
[5] => May
[6] => June
[7] => July
[8] => August
[9] => September
[10] => October
[11] => November
[12] => December
)
The result structure differs when requesting all calendars rather than one.
Example 2: A Different Input
<?php
echo cal_info(CAL_JULIAN)["calname"];
Output:
Julian
The result structure differs when requesting all calendars rather than one.
Practical Example: Applying It in a Program
<?php
$calendarInfo = cal_info(CAL_GREGORIAN); echo $calendarInfo["calname"] . ": " . count($calendarInfo["months"]);
Output:
Gregorian: 12
The result structure differs when requesting all calendars rather than one.
Important Behavior and Edge Cases
- Key behavior: The result structure differs when requesting all calendars rather than one.
- Environment: Requires the calendar extension.
Comparison With Related Functions
| Function | Purpose |
|---|---|
cal_info() | Returns calendar names and month information for supported calendars. |
cal_days_in_month() | cal_days_in_month() returns a month length; cal_info() describes supported calendars and month names. |
Common Beginner Mistakes
| Mistake | Correct Approach |
|---|---|
| Ignoring this behavior: The result structure differs when requesting all calendars rather than one | The result structure differs when requesting all calendars rather than one. |
Key Takeaways
- Returns calendar names and month information for supported calendars.
- The result structure differs when requesting all calendars rather than one.
- Requires the calendar extension.
Summary: The result structure differs when requesting all calendars rather than one.