date_diff

date_diff(DateTimeInterface $base1, DateTimeInterface $base2, bool $absolute = false): DateInterval

Computes the difference between two dates, returning a DateInterval.

C C23

C23
#include <stdio.h>
#include <time.h>
int main(void) {
    struct tm a = {0}, b = {0};
    a.tm_year = 2024 - 1900; a.tm_mon = 0; a.tm_mday = 15; // 2024-01-15
    b.tm_year = 2024 - 1900; b.tm_mon = 2; b.tm_mday = 10; // 2024-03-10
    time_t ta = timegm(&a), tb = timegm(&b); // UTC (POSIX extension, DST-safe)
    printf("%d\n", (int)((tb - ta) / 86400));
    return 0;
}
Output
55

C++ C++26

C++26
#include <chrono>
#include <cstdio>
#include <ctime>
int main() {
    std::tm a = {}, b = {};
    a.tm_year = 2024 - 1900; a.tm_mon = 0; a.tm_mday = 15; // 2024-01-15
    b.tm_year = 2024 - 1900; b.tm_mon = 2; b.tm_mday = 10; // 2024-03-10
    auto t1 = std::chrono::system_clock::from_time_t(timegm(&a)); // UTC (POSIX extension)
    auto t2 = std::chrono::system_clock::from_time_t(timegm(&b));
    std::printf("%d\n", (int)(std::chrono::duration_cast<std::chrono::hours>(t2 - t1).count() / 24));
    return 0;
}
Output
55

C# v11.0.100

v11.0.100
var d1 = new DateTime(2024, 1, 15);
var d2 = new DateTime(2024, 3, 10);
Console.WriteLine((d2 - d1).Days); // TimeSpan has no month component
Output
55

Go v1.27.4

v1.27.4
package main

import (
    "fmt"
    "time"
)

func main() {
    d1 := time.Date(2024, time.January, 15, 0, 0, 0, 0, time.UTC)
    d2 := time.Date(2024, time.March, 10, 0, 0, 0, 0, time.UTC)
    fmt.Println(int(d2.Sub(d1).Hours() / 24))
}
Output
55

Java v27.0.1

v27.0.1
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;

public class Main {
    public static void main(String[] args) {
        LocalDate d1 = LocalDate.of(2024, 1, 15);
        LocalDate d2 = LocalDate.of(2024, 3, 10);
        System.out.println(ChronoUnit.DAYS.between(d1, d2));
    }
}
Output
55

JavaScript v26.13.1

v26.13.1
const d1 = new Date(Date.UTC(2024, 0, 15));
const d2 = new Date(Date.UTC(2024, 2, 10));
console.log((d2 - d1) / 86400000);
Output
55

PHP v8.6.0

v8.6.0
$d1 = new DateTime("2024-01-15");
$d2 = new DateTime("2024-03-10");
echo date_diff($d1, $d2)->days;
Output
55

Python v3.15.2

v3.15.2
import datetime

d1 = datetime.date(2024, 1, 15)
d2 = datetime.date(2024, 3, 10)
print((d2 - d1).days)
Output
55

Rust v1.99.1

v1.99.1
fn main() {
    // chrono::Duration is an exact duration; num_days gives the total day count
    let d1 = chrono::NaiveDate::from_ymd_opt(2024, 1, 15).unwrap();
    let d2 = chrono::NaiveDate::from_ymd_opt(2024, 3, 10).unwrap();
    println!("{}", (d2 - d1).num_days());
}
Output
55

TypeScript v26.13.1

v26.13.1
const d1 = new Date(Date.UTC(2024, 0, 15));
const d2 = new Date(Date.UTC(2024, 2, 10));
console.log((d2.getTime() - d1.getTime()) / 86400000); // getTime() gives the millisecond difference
Output
55

Language differences

C: convert struct tm to timestamps first; timegm is a POSIX extension (available on macOS/glibc), while mktime uses local time and can skew across DST switch days.

C++: same as C — convert struct tm to timestamps first.

C#: uses TimeSpan (.Days).

Go: time.Duration has no month concept; the example converts via hours.

Java: the only one with a built-in year/month/day breakdown — Period.between(d1, d2) gives P1M24D (getMonths()/getDays()), the closest match to PHP's DateInterval.

JavaScript: millisecond difference with manual conversion.

PHP: date_diff returns a DateInterval: ->days is the total day count (55 here), and format("%m months %d days") also yields a human-readable breakdown like "1 months 24 days".

Python: timedelta (.days); months have unequal lengths, so a "month difference" is inherently fuzzy — the third-party dateutil.relativedelta is needed for that.

Rust: chrono::Duration::num_days.

TypeScript: same as JavaScript — millisecond difference with manual conversion.