Zero-dependency UTC date & time library for HolyPHP featuring the HDateTime class, ISO-8601 parsing and formatting, calendar arithmetic, and human-readable relative time.
bundled — works offlinezero dependenciesdatetime
Quick start
import "datetime";
$dt = dt_parse("2024-02-29T14:30:00Z");
println(dt_format($dt, "l, F j, Y g:i A"));
// Thursday, February 29, 2024 2:30 PM
$nextWeek = dt_add_days($dt, 7);
println($nextWeek->toIso8601());
// 2024-03-07T14:30:00Z
println(dt_relative($dt, $nextWeek));
// 7 days ago
public int $day (1–31, validated against month & leap year)
public int $hour (0–23)
public int $minute (0–59)
public int $second (0–59)
Constructor
new HDateTime(int $year, int $month, int $day, int $hour = 0, int $minute = 0, int $second = 0) — Validates all components and throws Exception on invalid dates or times.
Methods
toTimestamp(): int — Returns the Unix timestamp in seconds (supports pre-1970 negative timestamps).
format(string $pattern = "Y-m-d\\TH:i:s\\Z"): string — Formats the date/time using dt_format.
m (01–12), n (1–12), M (Jan–Dec), F (January–December)
d (01–31), j (1–31)
H (00–23), G (0–23), h (01–12), g (1–12)
i (00–59), s (00–59)
A (AM/PM), a (am/pm)
D (Sun–Sat), l (Sunday–Saturday), w (0–6)
U (Unix timestamp)
\ escapes the following character as a literal.
dt_add_days(HDateTime $dt, int $days): HDateTime Returns a new HDateTime shifted by $days.
dt_add_seconds(HDateTime $dt, int $seconds): HDateTime Returns a new HDateTime shifted by $seconds.
dt_diff_seconds(HDateTime $a, HDateTime $b): int Returns the signed difference in seconds ($a - $b).
dt_is_leap_year(int $year): bool Returns true if $year is a Gregorian leap year.
dt_days_in_month(int $year, int $month): int Returns the number of days in $month (1–12) for $year.
dt_relative(HDateTime $dt, $now = null): string Returns a human-readable relative time string compared to $now (defaults to dt_now()), such as "just now", "5 seconds ago", "in 3 minutes", "yesterday", "tomorrow", "2 hours ago", "4 days ago", "2 months ago", or "1 year ago".
Error Handling
All validation errors throw an Exception prefixed with datetime::
dt_parse("2024-02-30") -> datetime: invalid day 30 for 2024-02 (expected 1-29)
dt_parse("2023-02-29") -> datetime: invalid day 29 for 2023-02 (expected 1-28)