Oihana PHP Standards

toIso8601Time.php

Table of Contents

Functions

toIso8601Time()  : string
Converts a DateTimeInterface object to its ISO 8601 time string representation.

Functions

toIso8601Time()

Converts a DateTimeInterface object to its ISO 8601 time string representation.

toIso8601Time(DateTimeInterface $time) : string

This function generates a normalized ISO 8601 time string from any PHP DateTimeInterface object (DateTime or DateTimeImmutable). The output format is:

  • "THH:MM:SSZ" for UTC (zero offset)
  • "THH:MM:SS±HH:MM" for non-zero offsets

The 'T' prefix separates the time from a potential date component, and the timezone designator can be 'Z' for UTC or a ±HH:MM offset.

Parameters
$time : DateTimeInterface

The time to convert to ISO 8601 format

Tags
example
use org\iso\helpers\toIso8601Time;
use DateTimeImmutable;
use DateTimeZone;

$dt = new DateTimeImmutable('14:30:00', new DateTimeZone('UTC'));
echo toIso8601Time($dt); // "T14:30:00Z"

$dt2 = new DateTimeImmutable('08:15:30', new DateTimeZone('+02:00'));
echo toIso8601Time($dt2); // "T08:15:30+02:00"
link

ISO 8601 Time specification

PHP DateTimeInterface documentation

PHP DateTimeImmutable documentation

author

Marc Alcaraz (ekameleon)

since
1.0.1
Return values
string

The ISO 8601 time string, e.g. "T14:30:00Z" or "T08:15:30+02:00"

On this page

Search results