helpers
Table of Contents
Functions
- isIso8601Date() : bool
- Validates whether a string is a valid ISO 8601 calendar date.
- isIso8601DateTime() : bool
- Validates whether a string is a valid ISO 8601 date-time.
- isIso8601Duration() : bool
- Validates whether a string is a valid ISO 8601 duration format.
- isIso8601Interval() : bool
- Validates whether a string is a valid ISO 8601 time interval.
- isIso8601Recurrence() : bool
- Validates whether a string is a valid ISO 8601 recurring interval.
- isIso8601Time() : bool
- Validates whether a string is a valid ISO 8601 time format.
- toIso8601Date() : string
- Converts a DateTimeInterface object to its ISO 8601 calendar date string.
- toIso8601DateTime() : string
- Converts a DateTimeInterface object to its ISO 8601 date-time string.
- toIso8601Duration() : string
- Converts a DateInterval object to its ISO 8601 duration string representation.
- toIso8601Time() : string
- Converts a DateTimeInterface object to its ISO 8601 time string representation.
Functions
isIso8601Date()
Validates whether a string is a valid ISO 8601 calendar date.
isIso8601Date(string $date[, bool $strict = false ]) : bool
Accepted formats:
- Extended:
YYYY-MM-DD(e.g. "2026-05-14") - Basic :
YYYYMMDD(e.g. "20260514"), unless$strictis true
The function checks both the syntactic format and the calendar validity (i.e. February 30 is rejected, leap years are honored).
Parameters
- $date : string
-
The date string to validate
- $strict : bool = false
-
If true, only the extended format
YYYY-MM-DDis accepted (default: false)
Tags
Return values
bool —True if the string is a valid ISO 8601 date, false otherwise
isIso8601DateTime()
Validates whether a string is a valid ISO 8601 date-time.
isIso8601DateTime(string $value[, bool $strict = false ]) : bool
Accepted shape:
- Date :
YYYY-MM-DD(extended) - Sep. :
T(mandatory in strict mode, space allowed otherwise) - Time :
HH:MM:SS, optionally with fractional seconds (.fff...) - Offset: optional
Z,±HH:MMor±HHMM
The function checks both the syntactic format and the calendar validity (February 30 is rejected, leap years are honored).
Parameters
- $value : string
-
The date-time string to validate
- $strict : bool = false
-
If true, the
Tseparator is mandatory (default: false)
Tags
Return values
bool —True if the string is a valid ISO 8601 date-time, false otherwise
isIso8601Duration()
Validates whether a string is a valid ISO 8601 duration format.
isIso8601Duration(string $duration[, bool $strict = false ]) : bool
This function checks if a given string conforms to the ISO 8601 duration specification. The ISO 8601 duration format follows the pattern: P[n]Y[n]M[n]W[n]DT[n]H[n]M[n]S
Valid formats include:
- Date components: P[n]Y[n]M[n]W[n]D (years, months, weeks, days)
- Time components: PT[n]H[n]M[n]S (hours, minutes, seconds)
- Combined: P[n]Y[n]M[n]DT[n]H[n]M[n]S
- Zero duration: P0D, PT0S, P0Y, etc.
Rules:
- Must start with 'P' (period designator)
- 'T' separates date and time components (required if time components are present)
- At least one component must be present
- Components must be in correct order: Y, M, W/D, then T, then H, M, S
- Negative durations are not part of ISO 8601 standard but PHP accepts them
Parameters
- $duration : string
-
The duration string to validate
- $strict : bool = false
-
If true, uses regex validation; if false, uses PHP's DateInterval parser (default: false)
Tags
Return values
bool —True if the string is a valid ISO 8601 duration, false otherwise
isIso8601Interval()
Validates whether a string is a valid ISO 8601 time interval.
isIso8601Interval(string $value) : bool
An interval is two ISO 8601 expressions joined by a / separator, in one of
the following forms:
<start>/<end>— two strict date-times (e.g.2026-05-14T00:00:00Z/2026-05-15T00:00:00Z)<start>/<duration>— a start date-time and a duration (e.g.2026-05-14T00:00:00Z/P1D)<duration>/<end>— a duration and an end date-time (e.g.P1D/2026-05-15T00:00:00Z)
The single-<duration> short form (e.g. P1D alone) is intentionally
rejected: use Iso8601Duration for that case. Open intervals
(--/<end> and <start>/-- from ISO 8601:2019) are also rejected for now.
Date-time components are validated in strict mode (mandatory T separator).
Parameters
- $value : string
-
The interval string to validate
Tags
Return values
bool —True if the string is a valid ISO 8601 interval, false otherwise
isIso8601Recurrence()
Validates whether a string is a valid ISO 8601 recurring interval.
isIso8601Recurrence(string $value) : bool
The format is R[n]/<interval> where:
Ris the recurrence designator (required)nis an optional non-negative integer count (absent means infinite)<interval>is any valid ISO 8601 bounded interval — see isIso8601Interval()
Parameters
- $value : string
-
The recurrence string to validate
Tags
Return values
bool —True if the string is a valid ISO 8601 recurrence, false otherwise
isIso8601Time()
Validates whether a string is a valid ISO 8601 time format.
isIso8601Time(string $time[, bool $strict = false ]) : bool
This function checks if a given string conforms to the ISO 8601 time specification. ISO 8601 time format includes:
- Optional 'T' prefix
- Hours (00-23)
- Optional minutes (00-59)
- Optional seconds (00-59, can be fractional)
- Optional timezone offset (Z for UTC or ±HH:MM)
Examples of valid formats:
- "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)
Rules:
- Hours must be 00-23
- Minutes and seconds, if present, must be 00-59
- Fractional seconds are allowed
- Timezone, if present, must be 'Z' or ±HH:MM
Parameters
- $time : string
-
The time string to validate
- $strict : bool = false
-
If true, validates strictly with regex; if false, uses DateTimeImmutable parser (default: false)
Tags
Return values
bool —True if the string is a valid ISO 8601 time, false otherwise
toIso8601Date()
Converts a DateTimeInterface object to its ISO 8601 calendar date string.
toIso8601Date(DateTimeInterface $date[, bool $basic = false ]) : string
The output format is YYYY-MM-DD by default (ISO 8601 extended), or
YYYYMMDD if $basic is true (ISO 8601 basic).
The time and timezone components of the input are ignored; only the calendar date (year, month, day) in the object's own timezone is used.
Parameters
- $date : DateTimeInterface
-
The date to convert
- $basic : bool = false
-
If true, returns the basic format
YYYYMMDD(default: false)
Tags
Return values
string —The ISO 8601 date string, e.g. "2026-05-14" or "20260514"
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...]Zwhen offset is zeroYYYY-MM-DDTHH:MM:SS[.fff...]±HH:MMotherwise
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
Zsuffix (default: false)
Tags
Return values
string —The ISO 8601 date-time string
toIso8601Duration()
Converts a DateInterval object to its ISO 8601 duration string representation.
toIso8601Duration(DateInterval $interval) : string
This function generates a normalized ISO 8601 duration string from a PHP DateInterval object. The ISO 8601 duration format follows the pattern: P[n]Y[n]M[n]DT[n]H[n]M[n]S where P is the duration designator, T separates date and time components.
Components:
- Y: years
- M: months (before T) or minutes (after T)
- D: days
- H: hours
- M: minutes
- S: seconds
Only non-zero components are included in the output string. If all components are zero, returns "P0D" (zero duration).
Parameters
- $interval : DateInterval
-
The DateInterval object to convert
Tags
Return values
string —The ISO 8601 duration string (e.g., "P1Y2M3DT4H5M6S", "PT30M", "P0D")
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
Return values
string —The ISO 8601 time string, e.g. "T14:30:00Z" or "T08:15:30+02:00"