Oihana PHP Standards

toIso8601DateTime.php

Table of Contents

Functions

toIso8601DateTime()  : string
Converts a DateTimeInterface object to its ISO 8601 date-time string.

Functions

toIso8601DateTime()

Converts a DateTimeInterface object to its ISO 8601 date-time string.

toIso8601DateTime(DateTimeInterface $dt[, string $precision = TimePrecision::SECONDS ][, bool $zulu = false ]) : string

Output shape:

  • YYYY-MM-DDTHH:MM:SS[.fff...]Z when offset is zero
  • YYYY-MM-DDTHH:MM:SS[.fff...]±HH:MM otherwise

The offset is normalized to ±HH:MM (or Z for UTC) regardless of the underlying timezone object, mirroring the behavior of toIso8601Time().

Parameters
$dt : DateTimeInterface

The date-time to convert

$precision : string = TimePrecision::SECONDS

One of the TimePrecision constants (default: TimePrecision::SECONDS)

$zulu : bool = false

If true, the value is first converted to UTC and rendered with the Z suffix (default: false)

Tags
throws
InvalidArgumentException

If $precision is not a recognized value

example
use org\iso\helpers\toIso8601DateTime;
use org\iso\TimePrecision;
use DateTimeImmutable;
use DateTimeZone;

$dt = new DateTimeImmutable('2026-05-14 08:15:30', new DateTimeZone('+02:00'));

echo toIso8601DateTime($dt);                                    // "2026-05-14T08:15:30+02:00"
echo toIso8601DateTime($dt, TimePrecision::MILLISECONDS);       // "2026-05-14T08:15:30.000+02:00"
echo toIso8601DateTime($dt, TimePrecision::SECONDS, true);      // "2026-05-14T06:15:30Z"

$utc = new DateTimeImmutable('2026-05-14 08:15:30', new DateTimeZone('UTC'));
echo toIso8601DateTime($utc);                         // "2026-05-14T08:15:30Z"
link

ISO 8601 combined representations

author

Marc Alcaraz (ekameleon)

since
1.0.2
Return values
string

The ISO 8601 date-time string

On this page

Search results