Time\Duration

Time\Duration::fromSeconds(int $seconds): static; fromMilliseconds/fromMicroseconds/fromNanoseconds/fromMinutes/fromHours/fromIso8601DurationString(string $spec): static; $d->add(self $o): static; $d->sub(self $o): static; $d->multiplyBy(int|float $f): static; Duration::compare(self $a, self $b): int

New in PHP 8.6: a nanosecond-precision duration — attached to no instant, no timezone, comparable and arithmetic-friendly; upgrades the bare millisecond numbers of sleep/usleep/time_nanosleep into a typed duration.

C C23

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

int main(void) {
    struct timespec a = {2, 0};
    struct timespec b = {0, 500000000L};

    /* arithmetic is manual: carry nanoseconds into seconds by hand */
    struct timespec sum = {a.tv_sec + b.tv_sec, a.tv_nsec + b.tv_nsec};
    if (sum.tv_nsec >= 1000000000L) {
        sum.tv_sec += 1;
        sum.tv_nsec -= 1000000000L;
    }
    printf("%lld s %ld ns\n", (long long)sum.tv_sec, sum.tv_nsec);
    printf("%d\n", a.tv_sec < b.tv_sec || (a.tv_sec == b.tv_sec && a.tv_nsec < b.tv_nsec));
    return 0;
}
Output
2 s 500000000 ns
0

C++ C++26

C++26
#include <chrono>
#include <cstdio>

int main() {
    using namespace std::chrono_literals;
    auto a = 2s;
    auto b = 500ms;

    std::printf("%lld\n", static_cast<long long>(a.count()));
    std::printf("%lld\n", static_cast<long long>(b.count()));

    auto sum = a + b; // arithmetic preserves the finest common unit
    std::printf("%lld\n", static_cast<long long>(std::chrono::duration_cast<std::chrono::milliseconds>(sum).count()));

    std::printf("%lld\n", static_cast<long long>((a * 3).count()));
    std::printf("%d\n", (a < b) ? 1 : 0);
    return 0;
}
Output
2
500
2500
6
0

C# v11.0.100

v11.0.100
using System;

// TimeSpan is the .NET duration: factories, operators, comparison
var a = TimeSpan.FromSeconds(2);
var b = TimeSpan.FromMilliseconds(500);

Console.WriteLine(a);
Console.WriteLine(b.TotalMilliseconds);

var sum = a + b;
Console.WriteLine(sum);

Console.WriteLine(a * 3);
Console.WriteLine(TimeSpan.Compare(a, b) > 0);
Output
00:00:02
500
00:00:02.5000000
00:00:06
True

Go v1.27.4

v1.27.4
package main

import (
	"fmt"
	"time"
)

func main() {
	a := 2 * time.Second
	b := 500 * time.Millisecond

	// Duration is just an int64 count of nanoseconds under the hood
	fmt.Println(a)
	fmt.Println(b.Nanoseconds())

	sum := a + b
	fmt.Println(sum)

	// comparable, so the Go 1.21+ builtin min works directly
	fmt.Println(min(a, b))

	fmt.Println(a * 3)
	d, _ := time.ParseDuration("1h30m")
	fmt.Println(d)
}
Output
2s
500000000
2.5s
500ms
6s
1h30m0s

Java v27.0.1

v27.0.1
import java.time.Duration;

public class Main {
    public static void main(String[] args) {
        Duration a = Duration.ofSeconds(2);
        Duration b = Duration.ofMillis(500);
        System.out.println(a);
        System.out.println(b.toNanos());

        Duration sum = a.plus(b); // immutable: returns a new instance
        System.out.println(sum);

        System.out.println(a.multipliedBy(3));
        System.out.println(a.compareTo(b) > 0);
    }
}
Output
PT2S
500000000
PT2.5S
PT6S
true

JavaScript v26.13.1

v26.13.1
// no Duration type: milliseconds-in-a-number is the idiom
const a = 2000; // 2 s
const b = 500;
console.log(a + b);
console.log(a * 3);
console.log(Math.min(a, b));
console.log((a + b) / 1000); // converting back is manual math
Output
2500
6000
500
2.5

PHP v8.6.0

v8.6.0
use Time\Duration;

// the constructor is private: build through named constructors only
$a = Duration::fromSeconds(2);
$b = Duration::fromMilliseconds(500);
var_dump($a->seconds, $a->nanoseconds, $b->nanoseconds);

// arithmetic returns brand-new instances; the original never mutates
$sum = $a->add($b);
var_dump($sum->seconds, $sum->nanoseconds);

// nanosecond precision survives construction and arithmetic
$n = Duration::fromNanoseconds(1500)->add(Duration::fromNanoseconds(500));
var_dump($n->nanoseconds);

// Duration is comparable, so min()/max() work directly
var_dump(min($a, $b)->seconds);

// scaling, subtraction below zero, and ISO 8601 input
var_dump($a->multiplyBy(3)->seconds);
var_dump($b->sub($a)->negative);
var_dump(Duration::fromIso8601DurationString('PT1H30M')->seconds);

// hand the typed fields straight to time_nanosleep
$d = Duration::fromMilliseconds(10);
var_dump(time_nanosleep($d->seconds, $d->nanoseconds) === true);
Output
int(2)
int(0)
int(500000000)
int(2)
int(500000000)
int(2000)
int(0)
int(6)
bool(true)
int(5400)
bool(true)

Python v3.15.2

v3.15.2
from datetime import timedelta

# timedelta: microsecond-precision, fully overloadable duration
a = timedelta(seconds=2)
b = timedelta(milliseconds=500)
print(a)
print(b.total_seconds())

print(a + b)
print(a * 3)
print(min(a, b))
print(a > b)
Output
0:00:02
0.5
0:00:02.500000
0:00:06
0:00:00.500000
True

Rust v1.99.1

v1.99.1
use std::time::Duration;

fn main() {
    let a = Duration::from_secs(2);
    let b = Duration::from_millis(500);
    println!("{:?}", a);
    println!("{}", b.as_nanos());

    let sum = a + b; // arithmetic returns a new Duration
    println!("{:?}", sum);
    println!("{:?}", a * 3);

    // Ord is implemented, so a manual min is one comparison
    println!("{:?}", if a < b { a } else { b });
}
Output
2s
500000000
2.5s
6s
500ms

TypeScript v26.13.1

v26.13.1
// no Duration type: milliseconds-in-a-number is the idiom
const a: number = 2000; // 2 s
const b: number = 500;
console.log(a + b);
console.log(a * 3);
console.log(Math.min(a, b));
console.log((a + b) / 1000);
Output
2500
6000
500
2.5

Language differences

C: struct timespec (example): a two-field seconds+nanoseconds struct with POSIX nanosecond precision; arithmetic needs manual carrying and comparisons a hand-written two-level test — exactly what PHP's Duration class wraps up; nanosleep(2) is the very base of time_nanosleep.

C++: std::chrono::duration (example): a template whose tick precision is a compile-time decision (duration<int64,ratio>), with 2s+500ms auto-reduced to the finest common unit, milliseconds; chrono_literals suffixes make "2s" a first-class expression — PHP's named constructors are its runtime edition.

C#: System.TimeSpan (example): a value type of 100-ns ticks with FromSeconds/FromMilliseconds factories and full operator overloading; same name, same shape as Duration, even the scalar multiply.

Go: time.Duration (example): a plain int64 of nanoseconds AS the type (2*time.Second is constant-expression math), with ParseDuration parsing human strings like "1h30m" — the counterpart of fromIso8601DurationString with Go grammar; the Sleep family takes a Duration outright, so the old PHP habit of passing milliseconds simply cannot exist.

Java: java.time.Duration (example): immutable, nanosecond-precise, ISO-8601 output like PT2.5S (toNanos/multipliedBy/compareTo); differs from Duration mainly in factory naming (ofSeconds vs fromSeconds).

JavaScript: no duration type (example): setTimeout/setInterval all take a millisecond number, and add/scale/compare are bare-number math where one wrong unit is a 10x bug; the Temporal.Duration proposal (stage 3) is the equivalent but unsettled, so this entry does not depend on it.

PHP: the 8.6 RFC duration_class (example): a private constructor with a named-constructor family fromSeconds/fromNanoseconds/…/fromIso8601DurationString covering every unit and ISO 8601 (PT1H30M); the public properties seconds/nanoseconds/negative feed time_nanosleep directly (against the bare-number passing of usleep/sleep); it implements the comparison protocol, so min()/max()/comparison operators just work; attached to neither an instant nor a timezone, it measures "how long" rather than "when"; nanosecond precision holds across spans beyond 250 years (manual's wording).

Python: datetime.timedelta (example): an immutable microsecond-precision duration with full operator overloading; limited to microsecond precision and anchored in the datetime ecosystem (no standalone purely-typed duration), with total_seconds() returning a float — a half-way counterpart.

Rust: std::time::Duration (example): a value type of u64 seconds plus u32 nanoseconds, from_secs/from_millis factories, add/multiply ops, and Ord; the debug output "2.5s" is human-friendly; saturating arithmetic (no panic, no wrap) is how it splits duties with chrono.

TypeScript: same as JS (example): a number of milliseconds plus a type alias (type Millis = number) blocks "not a number" but not "wrong unit" — no answer until Temporal.Duration lands.