Skip to main content

Module datetime

Module datetime 

Source
Expand description

Date and time: date()/DateTime::format, DateTime parsing and timezones, strtotime, microtime.

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:

PHPRust
new DateTime($s, $tz) / DateTimeImmutableDateTime::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 < $bPartialOrd / 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).

Structs§

Abbreviation
A row of timelib’s abbreviation tables.
DateTime
A DateTime (or DateTimeImmutable): a timelib time with its zone.
Error
Why 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).
Interval
A DateInterval made by DateInterval::createFromDateString().
Message
One error or warning: where (byte offset in the trimmed input), the byte there, and the message.
Messages
timelib_error_container.
Modifier
A modifier string parsed once, to apply to many dates: what DateTimeImmutable::modify($modifier) does, without parsing the string again each time.
ParsedDate
What date_parse() returns.
Timestamp
A point in time, as gettimeofday reports it.
TzInfo
A timezone identifier resolved against PHP’s database.

Enums§

ErrorKind
The PHP exception (or error) a date/time operation raises.
TimeZone
A timezone, as DateTimeZone holds it.

Functions§

abbreviation_list
The rows of timezonemap.h (DateTimeZone::listAbbreviations() data).
date
date($format, $timestamp) in the default zone.
date_parse
date_parse($datetime).
gmdate
gmdate($format, $timestamp).
identifiers
timezone_identifiers_list() order: every identifier PHP knows, aliases included.
is_valid_id
timelib_timezone_id_is_valid.
microtime
microtime(): "0.12345600 1700000000" (%.8F %ld of the fraction and the seconds).
microtime_float
microtime(true): seconds since the epoch as a float.
strtotime
strtotime($datetime, $baseTimestamp) in the default zone: None where PHP returns false.
time
time().
version
timezone_version_get(): the version of the bundled database.