datetime
ModuleDate and time: date()/DateTime::format, DateTime parsing and timezones, strtotime, microtime.
Every function here is checked against the real PHP function by
bin/compat fuzz php-std (operations datetime.*).
This is a port of timelib (the library inside ext/date) and of the
parts of php_date.c around it, so that dates parse, normalise, convert
between zones and format exactly as PHP 8.5 does, quirks included:
| PHP | Rust |
|---|---|
new DateTime($s, $tz) / DateTimeImmutable | DateTime::parse (explicit "now"), DateTime::new (system clock) |
DateTime::createFromFormat($f, $s, $tz) | DateTime::from_format |
->format($f), date($f, $ts), gmdate($f, $ts) | DateTime::format, date, gmdate |
->modify($s) | DateTime::modify (DateTime), DateTime::modified (DateTimeImmutable) |
->setDate($y, $m, $d) | DateTime::set_date (DateTime), DateTime::with_date (DateTimeImmutable) |
->setTime($h, $i, $s, $us) | DateTime::set_time (DateTime), DateTime::with_time (DateTimeImmutable) |
->setTimezone($tz), ->getTimezone() | DateTime::set_timezone, DateTime::timezone |
->getTimestamp(), ->getOffset(), ->getMicrosecond() | DateTime::timestamp, DateTime::offset, DateTime::microsecond |
->add(DateInterval::createFromDateString($s)) | DateTime::add, Interval::from_date_string |
$a < $b | PartialOrd / Ord on DateTime |
new DateTimeZone($name), ->getName(), ->getOffset($dt) | TimeZone::new, TimeZone::name, TimeZone::offset_at |
strtotime($s, $base) | strtotime |
date_parse($s) | date_parse (ParsedDate::to_php) |
time(), microtime(true), microtime() | time, microtime_float, microtime |
timezone_version_get(), timezone_identifiers_list() | version, identifiers |
PHP keeps a default timezone in a global (date.timezone, UTC in
Appwrite). Here it is an argument: functions that use it take a
default TzInfo (TzInfo::utc for Appwrite).
Timezone data is PHP's own bundled database (version version), not
the system's, so results do not depend on the host.
Not modelled: DatePeriod, DateTime::diff(), DateTime::sub(),
intervals created from ISO 8601 durations, setISODate, idate,
mktime, strftime, getdate, sunrise/sunset functions;
date_default_timezone_set (pass the zone instead).
Contents
8 submodules, and what the module defines itself.
use php_std::datetime::DateTime;pub struct DateTimeA DateTime (or DateTimeImmutable): a timelib time with its zone.
== and < compare instants, as PHP's do (timelib_time_compare): the
same moment in two zones is equal.
PartialEq, PartialOrd.use php_std::datetime::Error;pub struct ErrorWhy a date/time operation failed: Error::php_class names the PHP
exception, Error::message_bytes is its message byte for byte (PHP
quotes the input verbatim, which need not be UTF-8).
Display.use php_std::datetime::Interval;pub struct Interval(RelTime)A DateInterval made by DateInterval::createFromDateString().
use php_std::datetime::Modifier;pub struct Modifier(Time)A modifier string parsed once, to apply to many dates: what
DateTimeImmutable::modify($modifier) does, without parsing the string
again each time.
use php_std::datetime::{DateTime, Modifier, Timestamp, TzInfo};
let next_day = Modifier::parse(b"+1 day")?;
let at = DateTime::parse(b"2026-03-28 12:00", Some(&php_std::datetime::TimeZone::new("Europe/Paris")?), &TzInfo::utc(), Timestamp { sec: 0, usec: 0 })?;
assert_eq!(at.modified_by(&next_day).format(b"Y-m-d H:i T"), b"2026-03-29 12:00 CEST");
assert_eq!(at.modified_by(&next_day), at.modified(b"+1 day")?);use php_std::datetime::ParsedDate;pub struct ParsedDateWhat date_parse() returns.
use php_std::datetime::Timestamp;pub struct TimestampA point in time, as gettimeofday reports it.
use php_std::datetime::ErrorKind;pub enum ErrorKindThe PHP exception (or error) a date/time operation raises.
use php_std::datetime::TimeZone;pub enum TimeZoneA timezone, as DateTimeZone holds it.
use php_std::datetime::date;pub fn date(format: &[u8], timestamp: i64, default: &TzInfo) -> Vec<u8>date($format, $timestamp) in the default zone.
use php_std::datetime::date_parse;pub fn date_parse(datetime: &[u8]) -> ParsedDatedate_parse($datetime).
use php_std::datetime::gmdate;pub fn gmdate(format: &[u8], timestamp: i64) -> Vec<u8>gmdate($format, $timestamp).
use php_std::datetime::microtime;pub fn microtime() -> Stringmicrotime(): "0.12345600 1700000000" (%.8F %ld of the fraction and the seconds).
use php_std::datetime::microtime_float;pub fn microtime_float() -> f64microtime(true): seconds since the epoch as a float.
use php_std::datetime::strtotime;pub fn strtotime(datetime: &[u8], base: i64, default: &TzInfo) -> Option<i64>strtotime($datetime, $baseTimestamp) in the default zone: None where
PHP returns false.
use php_std::datetime::time;pub fn time() -> i64time().
datetime::abbreviations
Own pageGenerated by data/timezonedb.py from timelib's timezonemap.h and fallbackmap.h: do not edit.
pub type Row = (&'static str, i32, i32, Option<&'static str>)An abbreviation row: name, DST flag, UTC offset in seconds, zone identifier.
datetime::format
Own pagedate_format() of php_date.c: the formatter behind date(),
gmdate() and DateTimeInterface::format().
use php_std::datetime::format::date_format;pub fn date_format(format: &[u8], t: &Time, localtime: bool) -> Vec<u8>date_format(format, t, localtime): PHP's output, byte for byte.
datetime::from_format
Own pagetimelib_parse_from_format with PHP's default format map: the parser
behind DateTime::createFromFormat() and date_parse_from_format().
use php_std::datetime::from_format::parse_from_format;pub fn parse_from_format(format: &[u8], string: &[u8]) -> (Time, Messages)timelib_parse_from_format.
datetime::parse
Own pagetimelib_strtotime (parse_date.re): PHP's free-form date/time parser,
used by strtotime(), date_parse(), new DateTime(),
DateTime::modify() and DateInterval::createFromDateString().
use php_std::datetime::parse::Message;pub struct MessageOne error or warning: where (byte offset in the trimmed input), the byte there, and the message.
use php_std::datetime::parse::Messages;pub struct Messagestimelib_error_container.
use php_std::datetime::parse::RelUnit;pub struct RelUnitA relative time word: its name, unit and multiplier (timelib_relunit).
use php_std::datetime::parse::Unit;pub enum UnitThe unit of a relative time word (timelib_relunit).
use php_std::datetime::parse::is_c_space;pub fn is_c_space(c: u8) -> boolC isspace in the C locale.
use php_std::datetime::parse::strtotime;pub fn strtotime(input: &[u8]) -> (Time, Messages)timelib_strtotime: the parsed (unnormalized) time and the errors and
warnings, positions relative to the input with surrounding whitespace
removed.
datetime::scanner
Own pageThe matching engine for timelib's re2c scanner: a Thompson NFA built
from the rule definitions of parse_date.re, simulated to find what the
re2c DFA finds: the longest match over all rules, the earliest rule on a
tie.
The simulation is cached as a lazily built DFA (each set of NFA states seen becomes a DFA state, each transition is computed once), so a format seen before is scanned at one table lookup per byte.
use php_std::datetime::scanner::ByteSet;pub struct ByteSet([u64; 4])A set of bytes.
use php_std::datetime::scanner::Lazy;pub struct Lazy(OnceLock<Nfa>, fn() -> Vec<P>)A lazily built NFA.
use php_std::datetime::scanner::Nfa;pub struct NfaAll rules compiled into one NFA.
use php_std::datetime::scanner::P;pub enum PA pattern (a re2c regular expression).
use php_std::datetime::scanner::alt;pub fn alt(ps: Vec<P>) -> PA pattern matching any one of ps.
use php_std::datetime::scanner::ci;pub fn ci(s: &str) -> P'...': an ASCII case-insensitive literal.
use php_std::datetime::scanner::digit;pub fn digit() -> PA pattern matching one ASCII digit.
use php_std::datetime::scanner::lit;pub fn lit(s: &str) -> P"...": a case-sensitive literal.
use php_std::datetime::scanner::opt;pub fn opt(p: P) -> PA pattern matching p or nothing.
use php_std::datetime::scanner::plus;pub fn plus(p: P) -> PA pattern matching p one or more times.
use php_std::datetime::scanner::range;pub fn range(a: u8, b: u8) -> P[a-b].
use php_std::datetime::scanner::ranges;pub fn ranges(rs: &[(u8, u8)]) -> PA union of byte ranges.
use php_std::datetime::scanner::rep;pub fn rep(p: P, min: u32, max: u32) -> PA pattern matching p from min to max times.
use php_std::datetime::scanner::seq;pub fn seq(ps: Vec<P>) -> PA pattern matching each of ps in turn.
use php_std::datetime::scanner::set;pub fn set(chars: &str) -> P[...] of the listed bytes.
use php_std::datetime::scanner::star;pub fn star(p: P) -> PA pattern matching p zero or more times.
datetime::timelib
Own pagetimelib's broken-down time and its arithmetic: timelib_time,
timelib_rel_time, tm2unixtime.c, unixtime2tm.c, dow.c,
interval.c (timelib_add).
Field names and semantics follow timelib (C's 64-bit arithmetic wraps where timelib's would, so extreme inputs give PHP's results too).
use php_std::datetime::timelib::Broken;pub struct BrokenThe date and time fields timelib_unixtime2gmt derives from a timestamp.
use php_std::datetime::timelib::RelTime;pub struct RelTimetimelib_rel_time.
use php_std::datetime::timelib::Time;pub struct Timetimelib_time.
Default.use php_std::datetime::timelib::date_from_epoch_days;pub fn date_from_epoch_days(epoch_days: i64) -> (i64, i64, i64)timelib_date_from_epoch_days.
use php_std::datetime::timelib::day_of_week;pub fn day_of_week(y: i64, m: i64, d: i64) -> i64timelib_day_of_week: 0 (Sunday) to 6.
use php_std::datetime::timelib::day_of_year;pub fn day_of_year(y: i64, m: i64, d: i64) -> i64timelib_day_of_year: 0-based.
use php_std::datetime::timelib::daynr_from_weeknr;pub fn daynr_from_weeknr(iy: i64, iw: i64, id: i64) -> i64timelib_daynr_from_weeknr.
use php_std::datetime::timelib::days_in_month;pub fn days_in_month(y: i64, m: i64) -> i64timelib_days_in_month.
use php_std::datetime::timelib::epoch_days_from_time;pub fn epoch_days_from_time(y: i64, m: i64, d: i64) -> i64timelib_epoch_days_from_time.
use php_std::datetime::timelib::hms_to_seconds;pub fn hms_to_seconds(h: i64, m: i64, s: i64) -> i64timelib_hms_to_seconds.
use php_std::datetime::timelib::is_leap;pub fn is_leap(y: i64) -> booltimelib_is_leap (C remainder semantics).
use php_std::datetime::timelib::iso_day_of_week;pub fn iso_day_of_week(y: i64, m: i64, d: i64) -> i64timelib_iso_day_of_week: 1 (Monday) to 7.
use php_std::datetime::timelib::isoweek_from_date;pub fn isoweek_from_date(y: i64, m: i64, d: i64) -> (i64, i64)timelib_isoweek_from_date: ISO week and ISO year.
use php_std::datetime::timelib::unixtime2gmt;pub fn unixtime2gmt(ts: i64) -> Brokentimelib_unixtime2gmt (the computation only).
use php_std::datetime::timelib::valid_date;pub fn valid_date(y: i64, m: i64, d: i64) -> booltimelib_valid_date.
use php_std::datetime::timelib::valid_time;pub fn valid_time(h: i64, i: i64, s: i64) -> booltimelib_valid_time.
pub const FIRST_DAY_OF_MONTH: i32 = 1TIMELIB_SPECIAL_FIRST_DAY_OF_MONTH: first day of.
pub const LAST_DAY_OF_MONTH: i32 = 2TIMELIB_SPECIAL_LAST_DAY_OF_MONTH: last day of.
pub const SPECIAL_DAY_OF_WEEK_IN_MONTH: u32 = 2TIMELIB_SPECIAL_DAY_OF_WEEK_IN_MONTH: first monday of.
pub const SPECIAL_LAST_DAY_OF_WEEK_IN_MONTH: u32 = 3TIMELIB_SPECIAL_LAST_DAY_OF_WEEK_IN_MONTH: last monday of.
pub const SPECIAL_WEEKDAY: u32 = 1TIMELIB_SPECIAL_WEEKDAY: +N weekdays.
pub const UNSET: i64 = -9999999TIMELIB_UNSET: a field the parser did not set.
pub const ZONETYPE_ABBR: u32 = 2TIMELIB_ZONETYPE_ABBR: an abbreviation (EST).
pub const ZONETYPE_ID: u32 = 3TIMELIB_ZONETYPE_ID: an identifier (Europe/Paris).
pub const ZONETYPE_NONE: u32 = 0TIMELIB_ZONETYPE_NONE: no zone.
pub const ZONETYPE_OFFSET: u32 = 1TIMELIB_ZONETYPE_OFFSET: a UTC offset (+05:30).
datetime::tz
Own pagePHP's bundled timezone database and timelib's lookups over it
(parse_tz.c, parse_posix.c, the abbreviation search of
parse_date.re).
The data is PHP's own (data/timezonedb.bin, extracted from the
timezonedb.h of the PHP version under test by data/timezonedb.py), so
zone names, transitions and abbreviations are PHP's, not the system's.
use php_std::datetime::tz::Abbreviation;pub struct AbbreviationA row of timelib's abbreviation tables.
use php_std::datetime::tz::Offset;pub struct OffsetWhat timelib_get_time_zone_info reports for an instant.
use php_std::datetime::tz::TzInfo;pub struct TzInfoA timezone identifier resolved against PHP's database.
The name is kept as it was requested (europe/paris stays lowercase), as
timelib does.
Debug, PartialEq.use php_std::datetime::tz::ZoneData;pub struct ZoneDataThe data of one zone (timelib_tzinfo without its name).
use php_std::datetime::tz::abbr_search;pub fn abbr_search(word: &[u8], gmtoffset: i64, isdst: i32) -> Option<Abbreviation>abbr_search: utc/gmt, then the first matching row (or the first
with gmtoffset when it is not -1), then the fallback map by offset
and DST flag.
use php_std::datetime::tz::abbreviations;pub fn abbreviations() -> impl Iterator<Item = Abbreviation>The rows of timezonemap.h (DateTimeZone::listAbbreviations() data).
use php_std::datetime::tz::identifiers;pub fn identifiers() -> impl Iterator<Item = &'static str>timezone_identifiers_list() order: every identifier PHP knows, aliases included.
use php_std::datetime::tz::is_valid_id;pub fn is_valid_id(name: &str) -> booltimelib_timezone_id_is_valid.
use php_std::datetime::tz::strcasecmp;pub fn strcasecmp(a: &[u8], b: &[u8]) -> i32timelib_strcasecmp: ASCII case-insensitive, byte-wise, then by length.
use php_std::datetime::tz::version;pub fn version() -> &'static strtimezone_version_get(): the version of the bundled database.
datetime::tzindex
Own pageGenerated by data/timezonedb.py from PHP's timezonedb.h: do not edit.
pub const VERSION: &str = "2026.3"timezone_version_get().