cal_days_in_month

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

Days in a month under a given calendar (calendar extension): CAL_GREGORIAN applies the Gregorian leap rule — divisible by 4 but not 100, or divisible by 400, gives February 29; 2024-02=29 vs 2023-02=28 is the canonical leap-year teaching sample, with 2000/1900 completing the century rules.

C C23

C23
/* no calendar API in libc: the Gregorian rule is one branch - 4-rule,
   100-rule, 400-rule */
#include <stdio.h>

static int days_in(int y, int m) {
    static const int d[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    if (m == 2 && ((y % 4 == 0 && y % 100 != 0) || y % 400 == 0)) {
        return 29;
    }
    return d[m - 1];
}

int main(void) {
    printf("%d\n", days_in(2024, 2)); /* 29 - leap year */
    printf("%d\n", days_in(2023, 2)); /* 28 */
    printf("%d\n", days_in(2000, 2)); /* 29 - the 400 rule wins */
    printf("%d\n", days_in(1900, 2)); /* 28 - the 100 rule bites */
    printf("%d %d\n", days_in(2024, 1), days_in(2024, 4)); /* 31 30 */
    return 0;
}
Output
29
28
29
28
31 30

C++ C++26

C++26
#include <iostream>

// std::chrono year_month_day_last{y/m/last}.day() is the typed C++20 answer,
// but this toolchain's libc++ build does not enable the calendar group - the
// Gregorian rule by hand (GCC's libstdc++ does enable it)
static int days_in(int y, int m) {
    static const int d[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    if (m == 2 && ((y % 4 == 0 && y % 100 != 0) || y % 400 == 0)) {
        return 29;
    }
    return d[m - 1];
}

int main() {
    std::cout << days_in(2024, 2) << "\n"; // 29 - leap year
    std::cout << days_in(2023, 2) << "\n"; // 28
    std::cout << days_in(2000, 2) << "\n"; // 29 - the 400 rule wins
    std::cout << days_in(1900, 2) << "\n"; // 28 - the 100 rule bites
    std::cout << days_in(2024, 1) << " " << days_in(2024, 4) << "\n"; // 31 30
    return 0;
}
Output
29
28
29
28
31 30

C# v11.0.100

v11.0.100
using System;

// DateTime.DaysInMonth: the 1:1 static analog (Gregorian only, no calendar arg)
Console.WriteLine(DateTime.DaysInMonth(2024, 2)); // 29 - leap year
Console.WriteLine(DateTime.DaysInMonth(2023, 2)); // 28
Console.WriteLine(DateTime.DaysInMonth(2000, 2)); // 29 - the 400 rule wins
Console.WriteLine(DateTime.DaysInMonth(1900, 2)); // 28 - the 100 rule bites
Console.WriteLine(DateTime.DaysInMonth(2024, 1) + " " + DateTime.DaysInMonth(2024, 4)); // 31 30
Output
29
28
29
28
31 30

Go v1.27.4

v1.27.4
package main

import (
	"fmt"
	"time"
)

// no DaysInMonth call: day 0 of month m+1 IS the last day of month m
func daysIn(y int, m time.Month) int {
	return time.Date(y, m+1, 0, 0, 0, 0, 0, time.UTC).Day()
}

func main() {
	fmt.Println(daysIn(2024, time.February)) // 29 - leap year
	fmt.Println(daysIn(2023, time.February)) // 28
	fmt.Println(daysIn(2000, time.February)) // 29 - the 400 rule wins
	fmt.Println(daysIn(1900, time.February)) // 28 - the 100 rule bites
	fmt.Println(daysIn(2024, time.January), daysIn(2024, time.April)) // 31 30
}
Output
29
28
29
28
31 30

Java v27.0.1

v27.0.1
import java.time.YearMonth;

public class Main {
    public static void main(String[] args) {
        // YearMonth.lengthOfMonth(): the 1:1 analog (leap rule handled internally)
        System.out.println(YearMonth.of(2024, 2).lengthOfMonth()); // 29 - leap year
        System.out.println(YearMonth.of(2023, 2).lengthOfMonth()); // 28
        System.out.println(YearMonth.of(2000, 2).lengthOfMonth()); // 29 - the 400 rule wins
        System.out.println(YearMonth.of(1900, 2).lengthOfMonth()); // 28 - the 100 rule bites
        System.out.println(YearMonth.of(2024, 1).lengthOfMonth() + " " + YearMonth.of(2024, 4).lengthOfMonth()); // 31 30
    }
}
Output
29
28
29
28
31 30

JavaScript v26.13.1

v26.13.1
// day 0 of month m (1-based m+1 here) IS the last day of month m
const daysIn = (y, m) => new Date(y, m, 0).getDate();
console.log(daysIn(2024, 2)); // 29 - leap year
console.log(daysIn(2023, 2)); // 28
console.log(daysIn(2000, 2)); // 29 - the 400 rule wins
console.log(daysIn(1900, 2)); // 28 - the 100 rule bites
console.log(daysIn(2024, 1), daysIn(2024, 4)); // 31 30
Output
29
28
29
28
31 30

PHP v8.6.0

v8.6.0
// cal_days_in_month(calendar, month, year): day count from the calendar
// extension - leap-year teaching in four assertions
var_dump(cal_days_in_month(CAL_GREGORIAN, 2, 2024)); // 29: divisible by 4, not by 100
var_dump(cal_days_in_month(CAL_GREGORIAN, 2, 2023)); // 28: the common case
var_dump(cal_days_in_month(CAL_GREGORIAN, 2, 2000)); // 29: the 400 rule wins
var_dump(cal_days_in_month(CAL_GREGORIAN, 2, 1900)); // 28: the 100 rule bites
echo cal_days_in_month(CAL_GREGORIAN, 1, 2024), "/", cal_days_in_month(CAL_GREGORIAN, 4, 2024), "\n"; // Jan has 31, Apr has 30
Output
int(29)
int(28)
int(29)
int(28)
31/30

Python v3.15.2

v3.15.2
import calendar

# calendar.monthrange(y, m)[1]: the 1:1 analog (second tuple slot = day count)
print(calendar.monthrange(2024, 2)[1])  # 29 - leap year
print(calendar.monthrange(2023, 2)[1])  # 28
print(calendar.monthrange(2000, 2)[1])  # 29 - the 400 rule wins
print(calendar.monthrange(1900, 2)[1])  # 28 - the 100 rule bites
print(calendar.monthrange(2024, 1)[1], calendar.monthrange(2024, 4)[1])  # 31 30
Output
29
28
29
28
31 30

Rust v1.99.1

v1.99.1
// std has no calendar math: the Gregorian rule is four lines - 4-rule,
// 100-rule, 400-rule (C++20 chrono's typed version ships in GCC's libstdc++)
fn days_in(y: i32, m: u32) -> u32 {
    match m {
        1 | 3 | 5 | 7 | 8 | 10 | 12 => 31,
        4 | 6 | 9 | 11 => 30,
        2 => {
            if (y % 4 == 0 && y % 100 != 0) || y % 400 == 0 {
                29
            } else {
                28
            }
        }
        _ => 0,
    }
}

fn main() {
    println!("{}", days_in(2024, 2)); // 29 - leap year
    println!("{}", days_in(2023, 2)); // 28
    println!("{}", days_in(2000, 2)); // 29 - the 400 rule wins
    println!("{}", days_in(1900, 2)); // 28 - the 100 rule bites
    println!("{} {}", days_in(2024, 1), days_in(2024, 4)); // 31 30
}
Output
29
28
29
28
31 30

TypeScript v26.13.1

v26.13.1
// day 0 of month m (1-based m+1 here) IS the last day of month m
const daysIn = (y: number, m: number): number => new Date(y, m, 0).getDate();
console.log(daysIn(2024, 2)); // 29 - leap year
console.log(daysIn(2023, 2)); // 28
console.log(daysIn(2000, 2)); // 29 - the 400 rule wins
console.log(daysIn(1900, 2)); // 28 - the 100 rule bites
console.log(daysIn(2024, 1), daysIn(2024, 4)); // 31 30
Output
29
28
29
28
31 30

Language differences

Four leap-year assertions: 2024-02=29 (the 4-rule), 2023-02=28 (common year), 2000-02=29 (the 400-rule beats the 100-rule), 1900-02=28 (the 100-rule bites) — the four samples teach the complete Gregorian rule, plus 31 vs 30 for big/small months; this is a calendar-extension entry (the extension is confirmed available, extension_loaded("calendar") is true — see the extension_loaded entry).

PHP: cal_days_in_month(CAL_GREGORIAN, $month, $year) — the calendar extension also ships CAL_JULIAN and other calendar constants plus the easter_date/easter_days algorithms; out-of-range month/year throws ValueError; pairs with checkdate (existing entry): know the day count first, then validate a concrete day.

Python: calendar.monthrange(y, m) returns (weekday of the 1st, day count) — index [1] for the days, the closest analog; calendar.isleap(y) tests leap years alone; note Python's month is 1-12 (same as PHP) but the calendar module's MONDAY..SUNDAY constants are 0-6.

Go: no ready-made function — the time.Date(y, m+1, 0, ...) day-0 normalization idiom ("day 0 of next month = last day of this month"); all time components are passed explicitly (no implicit now), so determinism is free.

Java: YearMonth.of(y, m).lengthOfMonth() is the 1:1 analog; LocalDate.lengthOfMonth() (once a date exists) or YearMonth.atEndOfMonth() are variants; non-ISO calendars go through chrono types like JapaneseChronology.

C#: DateTime.DaysInMonth(y, m) is the 1:1 static method; Gregorian only.

JavaScript/TypeScript: the idiom new Date(y, m, 0).getDate() — JS month constructor arguments are 0-11 (off by one from PHP/Python's 1-12!), so passing m itself means "next month", and day 0 rolls back to month end; one of JS's most famous date traps.

C/C++: libc has no calendar API — the handwritten three-branch rule is everything; C++20 std::chrono has the typed answer year_month_day_last{year(y)/month(m)/last}.day(), but this toolchain's clang libc++ does not enable the calendar group (GCC's libstdc++ does), so the example is handwritten.

Rust: std has no calendar math — the time/chrono crates do (chrono's NaiveDate::from_ymd_opt(y, m, 1).days_in_month()); the example handwrites the rule, isomorphic with C/C++.