toolkit

Dt

Dates and times: now, parse, format, diff and relative "ago" strings.

Dt:: Phlat\Toolkit\Dt 13 methods

Creation 6

  • setTimezone Sets the timezone Dt and PHP's own date functions use.
  • now The current time, in $timezone or the app timezone.
  • parse A value with no offset is read in $timezone (an int is a Unix timestamp); either way the result is expressed in it.
  • fromTimestamp A Unix timestamp as a date, in UTC.
  • time Current Unix timestamp.
  • microtime Seconds since the Unix epoch with a fractional part, for timing.

Formatting 4

  • format Formats a date with a PHP date format string.
  • gmt Formats a timestamp in GMT, e.g. for HTTP date headers.
  • ago Returns a human-readable relative time string, e.g. "3 minutes ago".
  • timestamp The date as a Unix timestamp.

Comparison 3

  • diff The interval from $a to $b.
  • isBefore Whether $a is earlier than $b.
  • isAfter Whether $a is later than $b.

Creation

Dt::setTimezone(string $timezone): void

Sets the timezone Dt and PHP's own date functions use.

Throws on an unknown identifier.

Parameters (1)
$timezone string
phlat/src/Toolkit/Dt.php:17

now

Dt::now(?string $timezone = null): DateTimeImmutable

The current time, in $timezone or the app timezone.

Parameters (1)
$timezone ?string = null

Returns DateTimeImmutable

phlat/src/Toolkit/Dt.php:23
Dt::parse(string|int $value, ?string $timezone = null): DateTimeImmutable

A value with no offset is read in $timezone (an int is a Unix timestamp); either way the result is expressed in it.

Both default to the app timezone.

Parameters (2)
$value string|int
$timezone ?string = null

Returns DateTimeImmutable

phlat/src/Toolkit/Dt.php:29
Dt::fromTimestamp(int $timestamp): DateTimeImmutable

A Unix timestamp as a date, in UTC.

Parameters (1)
$timestamp int

Returns DateTimeImmutable

phlat/src/Toolkit/Dt.php:37
Dt::time(): int

Current Unix timestamp.

Returns int

phlat/src/Toolkit/Dt.php:43
Dt::microtime(): float

Seconds since the Unix epoch with a fractional part, for timing.

Returns float

phlat/src/Toolkit/Dt.php:49

Formatting

Dt::format(DateTimeInterface $dt, string $format): string

Formats a date with a PHP date format string.

Parameters (2)
$dt DateTimeInterface
$format string

Returns string

phlat/src/Toolkit/Dt.php:57

gmt

Dt::gmt(string $format, ?int $timestamp = null): string

Formats a timestamp in GMT, e.g. for HTTP date headers.

Defaults to now.

Parameters (2)
$format string
$timestamp ?int = null

Returns string

phlat/src/Toolkit/Dt.php:63

ago

Dt::ago(DateTimeInterface|int $dt): string

Returns a human-readable relative time string, e.g. "3 minutes ago".

Parameters (1)
$dt DateTimeInterface|int

Returns string

phlat/src/Toolkit/Dt.php:69
Dt::timestamp(DateTimeInterface $dt): int

The date as a Unix timestamp.

Parameters (1)
$dt DateTimeInterface

Returns int

phlat/src/Toolkit/Dt.php:90

Comparison

Dt::diff(DateTimeInterface $a, DateTimeInterface $b): DateInterval

The interval from $a to $b.

Parameters (2)
$a DateTimeInterface
$b DateTimeInterface

Returns DateInterval

phlat/src/Toolkit/Dt.php:98
Dt::isBefore(DateTimeInterface $a, DateTimeInterface $b): bool

Whether $a is earlier than $b.

Parameters (2)
$a DateTimeInterface
$b DateTimeInterface

Returns bool

phlat/src/Toolkit/Dt.php:104
Dt::isAfter(DateTimeInterface $a, DateTimeInterface $b): bool

Whether $a is later than $b.

Parameters (2)
$a DateTimeInterface
$b DateTimeInterface

Returns bool

phlat/src/Toolkit/Dt.php:110