DateInterval
new DateInterval(string $duration) | DateTime::diff(DateTimeInterface $targetDate, bool $absolute = false): DateInterval
A date-interval/duration object: produced by DateTime::diff for the difference between two dates, or constructed manually from an ISO 8601 duration string (e.g. P2D, PT2H30M).
C C23
#include <stdio.h>
#include <string.h>
#include <time.h>
/* C has no interval type: struct tm → timegm to a UTC timestamp, seconds differenced into days by hand */
static time_t utc_ts(int year, int month, int day) {
struct tm t;
memset(&t, 0, sizeof t);
t.tm_year = year - 1900;
t.tm_mon = month - 1;
t.tm_mday = day;
return timegm(&t);
}
int main(void) {
time_t diff = utc_ts(2024, 3, 10) - utc_ts(2024, 1, 1);
printf("%lld\n", (long long)diff / 86400);
/* 2024 is a leap year: differencing real timestamps is automatically correct (Feb 29 is counted) */
time_t leap = utc_ts(2024, 3, 1) - utc_ts(2024, 2, 28);
printf("%lld\n", (long long)leap / 86400);
/* Duration: format the seconds by hand */
long long t = 2 * 3600 + 30 * 60;
printf("%lld:%02lld\n", t / 3600, (t / 60) % 60);
return 0;
}
69 2 2:30
C++ C++26
#include <cstdio>
#include <cstring>
#include <ctime>
// C++ has no interval type: same as C — struct tm → timegm to a UTC timestamp, seconds differenced by hand
static std::time_t utc_ts(int year, int month, int day) {
std::tm t{};
t.tm_year = year - 1900;
t.tm_mon = month - 1;
t.tm_mday = day;
return timegm(&t);
}
int main() {
long long diff = static_cast<long long>(utc_ts(2024, 3, 10) - utc_ts(2024, 1, 1));
std::printf("%lld\n", diff / 86400);
// 2024 is a leap year: differencing real timestamps is automatically correct (Feb 29 is counted)
long long leap = static_cast<long long>(utc_ts(2024, 3, 1) - utc_ts(2024, 2, 28));
std::printf("%lld\n", leap / 86400);
// Duration: format the seconds by hand
long long t = 2 * 3600 + 30 * 60;
std::printf("%lld:%02lld\n", t / 3600, (t / 60) % 60);
return 0;
}
69 2 2:30
C# v11.0.100
using System.Globalization;
// TimeSpan: pure elapsed time (.NET has no calendar-aware years/months/days component type); the subtraction operator yields a TimeSpan directly
var a = new DateTime(2024, 1, 1);
var b = new DateTime(2024, 3, 10);
Console.WriteLine((b - a).Days + " 天");
// 2024's leap day handled automatically
Console.WriteLine((new DateTime(2024, 3, 1) - new DateTime(2024, 2, 28)).Days + " 天");
// Years/months/days components (mirroring diff's %m/%d): not built in — count months and anchor differences by hand
var months = (b.Year - a.Year) * 12 + b.Month - a.Month - (b.Day < a.Day ? 1 : 0);
var anchor = a.AddMonths(months);
Console.WriteLine(months + " 月 " + (b - anchor).Days + " 天");
// Duration formatting: TimeSpan ships a ToString (02:30:00)
Console.WriteLine(TimeSpan.FromHours(2.5));
69 天 2 天 2 月 9 天 02:30:00
Go v1.27.4
package main
import (
"fmt"
"time"
)
func main() {
// time.Sub returns a time.Duration: pure elapsed time (nanosecond precision), no calendar year/month components
a := time.Date(2024, 1, 1, 0, 0, 0, 0, time.UTC)
b := time.Date(2024, 3, 10, 0, 0, 0, 0, time.UTC)
fmt.Println(int(b.Sub(a).Hours()/24), "天")
// 2024 is a leap year: Duration follows real time
leap := time.Date(2024, 3, 1, 0, 0, 0, 0, time.UTC).Sub(time.Date(2024, 2, 28, 0, 0, 0, 0, time.UTC))
fmt.Println(int(leap.Hours()/24), "天")
// Duration formatting: Duration ships a String ("2h30m0s"); the "2:30" style needs hand-writing
t := 2*time.Hour + 30*time.Minute
fmt.Printf("%d:%02d\n", int(t.Hours()), int(t.Minutes())%60)
}
69 天 2 天 2:30
Java v27.0.1
import java.time.Duration;
import java.time.LocalDate;
import java.time.Period;
import java.time.temporal.ChronoUnit;
public class Main {
public static void main(String[] args) {
// Period: calendar-aware years/months/days decomposition (the closest match to PHP DateInterval::diff's %m/%d)
Period p = Period.between(LocalDate.of(2024, 1, 1), LocalDate.of(2024, 3, 10));
System.out.println(p.getMonths() + " 月 " + p.getDays() + " 天");
// ChronoUnit.DAYS.between: the total days directly (mirroring ->days)
System.out.println(ChronoUnit.DAYS.between(LocalDate.of(2024, 1, 1), LocalDate.of(2024, 3, 10)) + " 天");
// 2024's leap day handled automatically
System.out.println(ChronoUnit.DAYS.between(LocalDate.of(2024, 2, 28), LocalDate.of(2024, 3, 1)) + " 天");
// Duration: exact elapsed time (mirroring ISO 8601's PT2H30M)
System.out.println(Duration.ofHours(2).plusMinutes(30));
}
}
2 月 9 天 69 天 2 天 PT2H30M
JavaScript v26.13.1
// Date has no interval type: difference the milliseconds by hand (days = ms / 86400000, UTC basis avoids DST shifts)
const a = Date.UTC(2024, 0, 1), b = Date.UTC(2024, 2, 10);
console.log((b - a) / 86400000 + " 天");
// 2024's leap day handled automatically (real timestamps underneath)
const leap = (Date.UTC(2024, 2, 1) - Date.UTC(2024, 1, 28)) / 86400000;
console.log(leap + " 天");
// The "2:30" duration: format the milliseconds by hand
const t = 2 * 3600000 + 30 * 60000;
console.log(Math.floor(t / 3600000) + ":" + String((t / 60000) % 60).padStart(2, "0"));
69 天 2 天 2:30
PHP v8.6.0
date_default_timezone_set("UTC");
// DateInterval's first identity: the return value of DateTime::diff — year/month/day/hour/minute/second components + days total
$a = new DateTime("2024-01-01");
$b = new DateTime("2024-03-10");
$diff = $a->diff($b);
echo $diff->format("%m 月 %d 天"), "\n";
echo $diff->days, "\n";
// Second identity: a manually constructed duration — an ISO 8601 duration string (P = date part, T = time part)
$i = new DateInterval("PT2H30M");
echo $i->format("%h:%I"), "\n";
// 2024 is a leap year: diff follows the real calendar, Feb 29 counted automatically
echo (new DateTime("2024-02-28"))->diff(new DateTime("2024-03-01"))->days, "\n";
2 月 9 天 69 2:30 2
Python v3.15.2
import datetime
# timedelta: pure elapsed time (total days/seconds), no calendar-aware year/month components
a = datetime.date(2024, 1, 1)
b = datetime.date(2024, 3, 10)
print((b - a).days, "天")
# 2024 is a leap year: timedelta follows real calendar days
print((datetime.date(2024, 3, 1) - datetime.date(2024, 2, 28)).days, "天")
# Duration representation: timedelta(hours=2, minutes=30); str always prints "2:30:00"
print(datetime.timedelta(hours=2, minutes=30))
69 天 2 天 2:30:00
Rust v1.99.1
// chrono: Duration is pure elapsed time; years/months/days component differences need hand calculation (anchoring, or the third-party datum crate)
use chrono::Datelike;
fn main() {
let a = chrono::NaiveDate::from_ymd_opt(2024, 1, 1).unwrap();
let b = chrono::NaiveDate::from_ymd_opt(2024, 3, 10).unwrap();
// Hand-counted months + anchor difference (mirroring PHP diff's %m/%d components)
let months = i64::from(b.year() - a.year()) * 12 + i64::from(b.month()) - i64::from(a.month())
- i64::from(b.day() < a.day());
let anchor = a.checked_add_months(chrono::Months::new(months as u32)).unwrap();
println!("{} 月 {} 天", months, (b - anchor).num_days());
println!("{} 天", (b - a).num_days());
// 2024's leap day handled automatically
let leap = chrono::NaiveDate::from_ymd_opt(2024, 3, 1).unwrap()
- chrono::NaiveDate::from_ymd_opt(2024, 2, 28).unwrap();
println!("{} 天", leap.num_days());
// Duration formatting: Duration has no built-in "2:30", hand-write it
let t = chrono::Duration::hours(2) + chrono::Duration::minutes(30);
println!("{}:{:02}", t.num_hours(), t.num_minutes() % 60);
}
2 月 9 天 69 天 2 天 2:30
TypeScript v26.13.1
// Date has no interval type: difference the milliseconds by hand (days = ms / 86400000, UTC basis avoids DST shifts)
const a = Date.UTC(2024, 0, 1), b = Date.UTC(2024, 2, 10);
const days: number = (b - a) / 86400000;
console.log(days + " 天");
// 2024's leap day handled automatically (real timestamps underneath)
const leap: number = (Date.UTC(2024, 2, 1) - Date.UTC(2024, 1, 28)) / 86400000;
console.log(leap + " 天");
// The "2:30" duration: format the milliseconds by hand
const t: number = 2 * 3600000 + 30 * 60000;
console.log(Math.floor(t / 3600000) + ":" + String((t / 60000) % 60).padStart(2, "0"));
69 天 2 天 2:30
Language differences
C: no interval type — convert struct tm to a UTC timestamp via timegm and hand-compute the day count (timegm is a POSIX extension, available on macOS/glibc); formatting hours/minutes is fully manual.
C++: same as C; with C++20's calendar chrono types you could diff year_month_day values, but under C++17 the C approach carries over.
C#: only TimeSpan (pure duration; .Days is whole days); the %m month / %d day breakdown has no built-in — the example hand-computes an anchor difference; TimeSpan.FromHours(2.5).ToString() prints 02:30:00.
Go: time.Duration is a nanosecond-precision pure duration with no year/month concept (month lengths vary); its String() prints 2h30m0s style.
Java: Period (calendar components) and Duration (exact duration) split the job — together they are the full counterpart of PHP's DateInterval; ChronoUnit.DAYS.between gives the total day count (->days).
JavaScript: no interval type — millisecond differences by hand; beware local-timezone construction skewing across DST, so the example pins UTC via Date.UTC; Intl.RelativeTimeFormat only covers relative-time wording.
PHP: DateInterval has two identities — the result of DateTime::diff (y/m/d/h/i/s components plus the days total) and a manually constructed duration (ISO 8601 duration: P for the date part / T for the time part, e.g. P1Y2M, PT2H30M); format() specifiers %m/%d/%h/%i/%a (%a is the total day count); note that a manually built new DateInterval("P2D") has days === false (no diff was computed), so the ->days total is only available through diff; diff decomposes by calendar (Jan 1 → Mar 10 = 2 months 9 days, with Feb 29 of the leap year counted automatically).
Python: timedelta is a pure duration (.days as the total) with no year/month components — "month difference" is inherently fuzzy; dateutil.relativedelta (relativedelta(d1, d2).months/.days) is the closest match to PHP's diff; str(timedelta(hours=2, minutes=30)) prints 2:30:00.
Rust: chrono::Duration is a pure duration (num_days); year/month/day components are hand-computed (anchor method shown); timestamp subtraction in UTC handles leap years automatically, as in C/C++.
TypeScript: same as JavaScript.