DateTime::modify

date_modify(DateTime $object, string $modifier): DateTime|false

Modifies a date with natural-language relative modifiers ('+1 day', 'next Thursday', '-2 months') — the only mainstream date object with this built in; its month-end overflow semantics differ sharply from the explicit arithmetic of other languages.

C C23

C23
#include <stdio.h>
#include <time.h>

int main(void) {
    /* no natural-language parser in C: mutate struct tm, then normalize via timegm */
    struct tm tm = {.tm_year = 2024 - 1900, .tm_mon = 0, .tm_mday = 15, .tm_hour = 10};
    tm.tm_mday += 1; /* +1 day */
    timegm(&tm);     /* normalize in place (UTC) */
    printf("%04d-%02d-%02d\n", tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday);
    return 0;
}
Output
2024-01-16

C++ C++26

C++26
#include <ctime>
#include <iomanip>
#include <iostream>

int main() {
    std::tm tm = {}; // same idiom as C: mutate, then normalize with timegm
    tm.tm_year = 2024 - 1900;
    tm.tm_mon = 0;
    tm.tm_mday = 15;
    tm.tm_mday += 1; // +1 day
    timegm(&tm);     // normalize in place (UTC)
    std::cout << std::put_time(&tm, "%Y-%m-%d") << "\n";
    return 0;
}
Output
2024-01-16

C# v11.0.100

v11.0.100
var d = new DateTime(2024, 1, 15, 10, 0, 0, DateTimeKind.Utc);
d = d.AddDays(1); // explicit arithmetic, no natural language
Console.WriteLine(d.ToString("yyyy-MM-dd"));
// AddMonths clamps to the last valid day instead of overflowing
Console.WriteLine(new DateTime(2024, 1, 31, 10, 0, 0, DateTimeKind.Utc).AddMonths(1).ToString("yyyy-MM-dd"));
Output
2024-01-16
2024-02-29

Go v1.27.4

v1.27.4
package main

import (
	"fmt"
	"time"
)

func main() {
	d := time.Date(2024, 1, 15, 10, 0, 0, 0, time.UTC)
	d = d.AddDate(0, 0, 1) // explicit arithmetic, no natural language
	fmt.Println(d.Format("2006-01-02"))
	// AddDate(0, 1, 0) normalizes Jan 31 + 1 month to Mar 2 (no clamping)
	fmt.Println(time.Date(2024, 1, 31, 10, 0, 0, 0, time.UTC).AddDate(0, 1, 0).Format("2006-01-02"))
}
Output
2024-01-16
2024-03-02

Java v27.0.1

v27.0.1
import java.time.LocalDateTime;

public class Main {
    public static void main(String[] args) {
        LocalDateTime d = LocalDateTime.of(2024, 1, 15, 10, 0);
        d = d.plusDays(1); // explicit arithmetic, no natural language
        System.out.println(d.toLocalDate());
        // plusMonths clamps to the last valid day instead of overflowing
        System.out.println(LocalDateTime.of(2024, 1, 31, 10, 0).plusMonths(1).toLocalDate());
    }
}
Output
2024-01-16
2024-02-29

JavaScript v26.13.1

v26.13.1
const d = new Date(Date.UTC(2024, 0, 15, 10));
d.setUTCDate(d.getUTCDate() + 1); // explicit arithmetic, no natural language
console.log(d.toISOString().slice(0, 10));
const m = new Date(Date.UTC(2024, 0, 31, 10));
m.setUTCMonth(m.getUTCMonth() + 1); // Jan 31 + 1 month overflows into March
console.log(m.toISOString().slice(0, 10));
Output
2024-01-16
2024-03-02

PHP v8.6.0

v8.6.0
date_default_timezone_set('UTC');
$d = new DateTime('2024-01-15 10:00:00', new DateTimeZone('UTC'));
$d->modify('+1 day'); // natural-language relative modifier
echo $d->format('Y-m-d'), PHP_EOL;
$d->modify('next Thursday');
echo $d->format('Y-m-d'), PHP_EOL;
$m = new DateTime('2024-01-31 10:00:00', new DateTimeZone('UTC'));
$m->modify('+1 month'); // Feb 31 does not exist: overflows into March
echo $m->format('Y-m-d'), PHP_EOL;
Output
2024-01-16
2024-01-18
2024-03-02

Python v3.15.2

v3.15.2
from calendar import monthrange
from datetime import date, timedelta

d = date(2024, 1, 15) + timedelta(days=1)  # explicit arithmetic, no natural language
print(d)
y, m = 2024, 1
y2, m2 = (y + 1, 1) if m == 12 else (y, m + 1)
# the stdlib month idiom clamps to the last valid day (no overflow)
print(date(y2, m2, min(31, monthrange(y2, m2)[1])))
Output
2024-01-16
2024-02-29

Rust v1.99.1

v1.99.1
use chrono::{Days, Months, NaiveDate};

fn main() {
    let d = NaiveDate::from_ymd_opt(2024, 1, 15).unwrap() + Days::new(1);
    println!("{}", d);
    // chrono's Months clamps to the last valid day instead of overflowing
    let m = NaiveDate::from_ymd_opt(2024, 1, 31)
        .unwrap()
        .checked_add_months(Months::new(1))
        .unwrap();
    println!("{}", m);
}
Output
2024-01-16
2024-02-29

TypeScript v26.13.1

v26.13.1
const d: Date = new Date(Date.UTC(2024, 0, 15, 10));
d.setUTCDate(d.getUTCDate() + 1); // explicit arithmetic, no natural language
console.log(d.toISOString().slice(0, 10));
const m: Date = new Date(Date.UTC(2024, 0, 31, 10));
m.setUTCMonth(m.getUTCMonth() + 1); // Jan 31 + 1 month overflows into March
console.log(m.toISOString().slice(0, 10));
Output
2024-01-16
2024-03-02

Language differences

C: C has no natural-language date modifier — the idiom is mutating struct tm fields and normalizing with timegm (example: +1 day); overflow also rolls silently (a tm_mon of 12 spills into the next year), so clamping must be hand-written; timegm needs a feature test macro and an explicit #include <time.h>.

C++: same struct tm + timegm idiom as C (example); C++20 <chrono> calendar types offer year_month_day arithmetic but no natural language; only the third-party date.h library provides modify-like sugar.

C#: AddDays/AddMonths are the explicit-arithmetic family, with no natural language; measured: AddMonths(1) on 2024-01-31 clamps to Feb 29 (leap year), unlike PHP's modify which overflows to Mar 2.

Go: AddDate(0,0,1) is the explicit-arithmetic family; measured: AddDate(0,1,0) on 2024-01-31 normalizes to Mar 2 (fields are added, then the overflow carries), the same camp as PHP.

Java: plusDays/plusMonths are the explicit-arithmetic family; measured: plusMonths(1) on 2024-01-31 clamps to Feb 29 (leap year).

JavaScript: setUTCDate does day arithmetic and there is no month-level API; measured: setUTCMonth(+1) on 2024-01-31 overflows into Mar 2 (setting the month overflows the day field), the same camp as PHP; always use the UTC variants or the result drifts with the local timezone.

PHP: DateTime::modify (date_modify is its functional twin) is the only mainstream date object with natural-language relative syntax built in: '+1 day', 'next Thursday', '-2 months' (example's first three lines); month-end trap: 2024-01-31 +1 month overflows to Mar 2 (measured), and '-2 months' similarly spills from Nov 31 to Dec 1 — for billing/reconciliation use DateTime::add(new DateInterval('P1M')) or clamp the base date first.

Python: the stdlib only has timedelta day-level arithmetic (example line one); there is no built-in month arithmetic — the idiom is calendar.monthrange clamping (example line two, measured Feb 29), i.e. the clamping camp, not the overflow camp; only third-party dateutil.relativedelta has '+1 month' semantics.

Rust: chrono's Days/Months types are the explicit-arithmetic family; measured: checked_add_months(Months::new(1)) on 2024-01-31 clamps to Feb 29; the checked_* family returns Option, forcing you to handle the result.

TypeScript: same setUTCDate/setUTCMonth behavior as JavaScript (the overflow camp); note that dayjs .add(1, 'month') takes the clamping camp, opposite to native setUTCMonth — mind the choice when picking libraries.