Iso8601Time
Represents and manipulates ISO 8601 time strings (without a date component).
This class provides a convenient wrapper around PHP's DateTimeImmutable / DateTimeInterface
for handling times expressed in ISO 8601 format. It maintains synchronization between
the ISO 8601 string representation and the internal immutable time object.
ISO 8601 time examples:
- "T14:30:00Z" → 14:30:00 UTC
- "T08:15:30+02:00" → 08:15:30 in UTC+2
- "T23:59:59" → 23:59:59 local time (no offset)
The time string may include hours, minutes, seconds (optionally fractional), and a timezone offset ('Z' for UTC or ±HH:MM).
Example usage:
use org\iso\Iso8601Time;
// From ISO 8601 string
$time = new Iso8601Time('T14:30:00Z');
echo $time->hours; // 14
echo $time->minutes; // 30
echo $time->seconds; // 0
echo $time->iso; // "T14:30:00Z"
// From DateTimeInterface
$dt = new DateTimeImmutable('08:15:30', new DateTimeZone('+02:00'));
$time2 = new Iso8601Time($dt);
echo $time2->toDateTime()->format('H:i:s P'); // "08:15:30 +02:00"
// Modify the ISO string
$time2->iso = 'T12:00:00Z';
// Modify using DateTimeInterface
$time2->time = new DateTimeImmutable('23:59:59');
Tags
Table of Contents
Constants
- FORMAT : string = 'H:i:s'
- The full time format.
- HOUR : string = 'H'
- The 24-hour format of an hour with leading zeros
- MINUTE : string = 'i'
- The 'minute' format character.
- PATTERN : string = '/^T?' . '([01]\d|2[0-3])' . '(?::([0-5]\d))?'...
- ISO 8601 time pattern: - Optional 'T' prefix - Hours, optional minutes, optional seconds - Optional timezone offset or 'Z'
- SECOND : string = 's'
- The 'second' format character.
- TIME : string = 'T'
- Time designator (separates date and time components).
- TIME_ZERO : string = 'T00:00:00'
- Zero iso time constant.
- TIME_ZONE : string = 'Z'
- The Timezone designator.
- ZERO : string = '00:00:00'
- Zero time constant.
Properties
- $hours : int
- Gets the hours component (0–23).
- $iso : string
- ISO string representation (e.g. "T14:30:00Z").
- $minutes : int
- Gets the minutes component (0–59).
- $seconds : int
- Gets the seconds component (0–59).
- $time : DateTimeInterface
- The internal immutable time representation.
- $timezone : DateTimeZone|null
- Gets the timezone object (or null if none specified).
- $_iso : string
- $_time : DateTimeImmutable
- The internal DateTimeImmutable representing the time.
Methods
- __construct() : mixed
- Creates a new Iso8601Time instance.
- __toString() : string
- String cast returns the ISO representation.
Constants
FORMAT
The full time format.
public
string
FORMAT
= 'H:i:s'
HOUR
The 24-hour format of an hour with leading zeros
public
string
HOUR
= 'H'
MINUTE
The 'minute' format character.
public
string
MINUTE
= 'i'
Minutes with leading zeros
PATTERN
ISO 8601 time pattern: - Optional 'T' prefix - Hours, optional minutes, optional seconds - Optional timezone offset or 'Z'
public
string
PATTERN
= '/^T?' . '([01]\d|2[0-3])' . '(?::([0-5]\d))?' . '(?::([0-5]\d(?:\.\d+)?))?' . '(Z|[+\-](?:[01]\d|2[0-3]):?[0-5]\d)?$/'
SECOND
The 'second' format character.
public
string
SECOND
= 's'
Seconds with leading zeros
TIME
Time designator (separates date and time components).
public
string
TIME
= 'T'
TIME_ZERO
Zero iso time constant.
public
string
TIME_ZERO
= 'T00:00:00'
TIME_ZONE
The Timezone designator.
public
string
TIME_ZONE
= 'Z'
ZERO
Zero time constant.
public
string
ZERO
= '00:00:00'
Properties
$hours read-only virtual
Gets the hours component (0–23).
public
int
$hours
Hooks
public
int
get
$iso virtual
ISO string representation (e.g. "T14:30:00Z").
public
string
$iso
Hooks
public
string
get
public
set
$minutes read-only virtual
Gets the minutes component (0–59).
public
int
$minutes
Hooks
public
int
get
$seconds read-only virtual
Gets the seconds component (0–59).
public
int
$seconds
Hooks
public
int
get
$time virtual
The internal immutable time representation.
public
DateTimeInterface
$time
Hooks
public
DateTimeInterface
get
public
set
$timezone read-only virtual
Gets the timezone object (or null if none specified).
public
DateTimeZone|null
$timezone
Hooks
public
DateTimeZone|null
get
$_iso
private
string
$_iso
$_time
The internal DateTimeImmutable representing the time.
private
DateTimeImmutable
$_time
Methods
__construct()
Creates a new Iso8601Time instance.
public
__construct([string|DateTimeImmutable|null $time = null ]) : mixed
Parameters
- $time : string|DateTimeImmutable|null = null
-
ISO string, DateTime, or null for "T00:00:00"
Tags
__toString()
String cast returns the ISO representation.
public
__toString() : string