date_create_immutable / date_create_immutable_from_format
date_create_immutable(string $datetime = "now", ?DateTimeZone $timezone = null): DateTimeImmutable|false · date_create_immutable_from_format(string $format, string $datetime, ?DateTimeZone $timezone = null): DateTimeImmutable|false
Immutable date objects (DateTimeImmutable): modify returns a new instance and the original never changes — versus the mutable DateTime; java.time/datetime/time.Time share the philosophy across languages, with JS Date as the mutable exception that must clone to protect itself.
C C23
#include <stdio.h>
#include <time.h>
int main(void) {
/* struct tm copies by value: mutating a copy leaves the original untouched */
struct tm dt = {0};
dt.tm_year = 2026 - 1900;
dt.tm_mon = 10; /* November is 0-based */
dt.tm_mday = 25;
struct tm tomorrow = dt; /* value copy is C's "immutability" */
tomorrow.tm_mday += 1;
timegm(&tomorrow); /* normalize fields in place */
char buf[16], buf2[16];
strftime(buf, sizeof(buf), "%Y-%m-%d", &dt);
strftime(buf2, sizeof(buf2), "%Y-%m-%d", &tomorrow);
printf("%s\n", buf);
printf("%s (original unchanged)\n", buf2);
/* overflow normalizes: Jan 31 + 1 month lands on Mar 3 (PHP-like!) */
struct tm overflow = dt;
overflow.tm_mon = 0;
overflow.tm_mday = 31;
overflow.tm_mon += 1;
timegm(&overflow);
strftime(buf2, sizeof(buf2), "%Y-%m-%d", &overflow);
printf("%s\n", buf2);
return 0;
}
2026-11-25 2026-11-26 (original unchanged) 2026-03-03
C++ C++26
#include <chrono>
#include <iostream>
int main() {
using namespace std::chrono;
// year_month_day is immutable: day arithmetic returns a NEW value
year_month_day dt{2026y, November, 25d};
year_month_day tomorrow = sys_days{dt} + days{1};
std::cout << static_cast<int>(dt.year()) << '-' << unsigned(dt.month())
<< '-' << unsigned(dt.day()) << "\n";
std::cout << static_cast<int>(tomorrow.year()) << '-' << unsigned(tomorrow.month())
<< '-' << unsigned(tomorrow.day()) << " (original unchanged)\n";
// serial day count normalizes overflow: Jan 31 + 31 days lands on Mar 3
sys_days overflow = sys_days{2026y / January / 31} + days{31};
year_month_day ymd{overflow};
std::cout << static_cast<int>(ymd.year()) << '-' << unsigned(ymd.month())
<< '-' << unsigned(ymd.day()) << "\n";
return 0;
}
2026-11-25 2026-11-26 (original unchanged) 2026-3-3
C# v11.0.100
// DateTime is a value type: AddDays returns a NEW value
var dt = new DateTime(2026, 11, 25);
var tomorrow = dt.AddDays(1);
Console.WriteLine(dt.ToString("yyyy-MM-dd", System.Globalization.CultureInfo.InvariantCulture));
Console.WriteLine(tomorrow.ToString("yyyy-MM-dd", System.Globalization.CultureInfo.InvariantCulture) + " (original unchanged)");
// month arithmetic clamps on overflow: Jan 31 + 1 month = Feb 28
Console.WriteLine(new DateTime(2026, 1, 31).AddMonths(1).ToString("yyyy-MM-dd", System.Globalization.CultureInfo.InvariantCulture));
2026-11-25 2026-11-26 (original unchanged) 2026-02-28
Go v1.27.4
package main
import (
"fmt"
"time"
)
func main() {
// time.Time is immutable: AddDate returns a NEW value
dt := time.Date(2026, 11, 25, 8, 30, 0, 0, time.UTC)
tomorrow := dt.AddDate(0, 0, 1)
fmt.Println(dt.Format("2006-01-02"))
fmt.Println(tomorrow.Format("2006-01-02"), "(original unchanged)")
// month overflow normalizes: Jan 31 + 1 month lands on Mar 3 (PHP-like!)
fmt.Println(time.Date(2026, 1, 31, 0, 0, 0, 0, time.UTC).AddDate(0, 1, 0).Format("2006-01-02"))
}
2026-11-25 2026-11-26 (original unchanged) 2026-03-03
Java v27.0.1
import java.time.LocalDate;
public class Main {
public static void main(String[] args) {
// java.time is immutable: plusDays returns a NEW object
LocalDate dt = LocalDate.of(2026, 11, 25);
LocalDate tomorrow = dt.plusDays(1);
System.out.println(dt);
System.out.println(tomorrow + " (original unchanged)");
// month arithmetic clamps on overflow: Jan 31 + 1 month = Feb 28
System.out.println(LocalDate.of(2026, 1, 31).plusMonths(1));
}
}
2026-11-25 2026-11-26 (original unchanged) 2026-02-28
JavaScript v26.13.1
// JS Date is MUTABLE: setDate mutates in place (the contrast!)
const d = new Date(Date.UTC(2026, 10, 25));
const clone = new Date(d); // clone before mutating: the manual immutability
clone.setUTCDate(clone.getUTCDate() + 1);
console.log(d.toISOString().slice(0, 10), "(original still Nov 25)");
console.log(clone.toISOString().slice(0, 10), "(clone moved to Nov 26)");
2026-11-25 (original still Nov 25) 2026-11-26 (clone moved to Nov 26)
PHP v8.6.0
date_default_timezone_set('UTC');
// modify() returns a NEW object; the original is untouched
$dt = date_create_immutable('2026-11-25 08:30:00');
$tomorrow = $dt->modify('+1 day');
echo $dt->format('Y-m-d'), "\n";
echo $tomorrow->format('Y-m-d'), " (original unchanged)\n";
// month arithmetic with overflow: Jan 31 + 1 month lands on Mar 3
echo date_create_immutable('2026-01-31')->modify('+1 month')->format('Y-m-d'), "\n";
// parse with an explicit format into the same immutable class
echo date_create_immutable_from_format('d/m/Y', '25/11/2026')->format('Y-m-d'), "\n";
2026-11-25 2026-11-26 (original unchanged) 2026-03-03 2026-11-25
Python v3.15.2
# datetime.date is immutable: + timedelta returns a NEW object
from datetime import date, timedelta
dt = date(2026, 11, 25)
tomorrow = dt + timedelta(days=1)
print(dt)
print(tomorrow, "(original unchanged)")
# replace() is the immutable way to swap a field (returns a new object)
print(dt.replace(month=12))
2026-11-25 2026-11-26 (original unchanged) 2026-12-25
Rust v1.99.1
use chrono::{Datelike, Duration, NaiveDate};
fn main() {
// NaiveDate is immutable: + Duration returns a NEW value
let dt = NaiveDate::from_ymd_opt(2026, 11, 25).unwrap();
let tomorrow = dt + Duration::days(1);
println!("{}", dt.format("%Y-%m-%d"));
println!("{} (original unchanged)", tomorrow.format("%Y-%m-%d"));
// with_month returns Option (same-day month swap when valid)
println!("{}", dt.with_month(12).unwrap().format("%Y-%m-%d"));
}
2026-11-25 2026-11-26 (original unchanged) 2026-12-25
TypeScript v26.13.1
// JS Date is MUTABLE: setDate mutates in place (the contrast!)
const d: Date = new Date(Date.UTC(2026, 10, 25));
const clone: Date = new Date(d); // clone before mutating: the manual immutability
clone.setUTCDate(clone.getUTCDate() + 1);
console.log(d.toISOString().slice(0, 10), "(original still Nov 25)");
console.log(clone.toISOString().slice(0, 10), "(clone moved to Nov 26)");
2026-11-25 (original still Nov 25) 2026-11-26 (clone moved to Nov 26)
Language differences
C: struct tm's by-value copy is C's "immutability" (example); timegm/mktime normalize overflow (2/31 → 3/3), matching PHP's rolling semantics.
C++: the C++20 chrono year_month_day is immutable (example); month addition at the ymd level can produce invalid dates (2026-02-31) and must go through sys_days serial days to normalize — rolling or clamping is the developer's explicit choice.
C#: DateTime is a value type, so AddDays/AddMonths naturally return new values (example); AddMonths clamps to the end of the month (1/31 + 1 month = 2/28) — same camp as Java, opposite to the rolling camp of PHP/Go.
Go: time.Time is immutable (example); AddDate normalizes month overflow (1/31 + 1 month = 3/3) — the same rolling semantics as PHP.
Java: the whole java.time family is immutable (example); plusMonths clamps to the month's end — the immutability philosophy matches DateTimeImmutable while the overflow strategy is opposite to PHP (clamp vs roll); keep the two questions separate.
JavaScript: Date is a mutable object — setDate mutates in place (example); "immutability" means cloning with new Date(d) before mutating, the counterexample among these languages; the Temporal proposal (TC39 stage 3) exists precisely to add immutable dates.
PHP: DateTimeImmutable shares the philosophy of A48's readonly — state cannot change in place; modify/add/setDate all return new instances (the mutable DateTime's modify edits in place); the +1 month overflow roll (1/31 → 3/3) is one of two "add a month" semantics (the other being Java/C#'s end-of-month clamp); date_create_immutable_from_format combines explicit-format parsing with immutability in one step.
Python: datetime.date/datetime are immutable (example); + timedelta and replace() both return new objects; the stdlib has no month addition — use dateutil or manual arithmetic.
Rust: chrono's NaiveDate is immutable (example); field-changing APIs like with_month return Option, surfacing invalid values explicitly and turning runtime surprises into compile-time types.
TypeScript: same mutable Date plus cloning as JavaScript (example); the type layer cannot prevent in-place mutation — real guarantees need a readonly wrapper or Temporal.