Oihana PHP Standards

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 $strict is 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-DD is accepted (default: false)

Tags
example
isIso8601Date('2026-05-14');         // true
isIso8601Date('20260514');           // true (basic)
isIso8601Date('20260514', true);     // false (strict rejects basic)
isIso8601Date('2026-02-30');         // false (invalid calendar date)
isIso8601Date('2024-02-29');         // true  (leap year)
isIso8601Date('2023-02-29');         // false (not a leap year)
isIso8601Date('2026-5-14');          // false (month not zero-padded)
link

ISO 8601 calendar dates

author

Marc Alcaraz (ekameleon)

since
1.0.2
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:MM or ±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 T separator is mandatory (default: false)

Tags
example
isIso8601DateTime('2026-05-14T08:15:30Z');         // true
isIso8601DateTime('2026-05-14T08:15:30+02:00');    // true
isIso8601DateTime('2026-05-14T08:15:30.123Z');     // true (milliseconds)
isIso8601DateTime('2026-05-14 08:15:30');          // true (space separator)
isIso8601DateTime('2026-05-14 08:15:30', true);    // false (strict requires T)
isIso8601DateTime('2026-02-30T00:00:00Z');         // false (invalid calendar date)
isIso8601DateTime('2026-05-14T24:00:00');          // false (invalid hour)
link

ISO 8601 combined representations

author

Marc Alcaraz (ekameleon)

since
1.0.2
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
example
validateIso8601Duration('P1Y2M3D'); // true
validateIso8601Duration('PT4H30M'); // true
validateIso8601Duration('P1W'); // true
validateIso8601Duration('P0D'); // true
validateIso8601Duration('INVALID'); // false
validateIso8601Duration('P'); // false (no components)
validateIso8601Duration('1Y2M'); // false (missing P)
// Strict mode validation (regex-based)
validateIso8601Duration('P1Y2M3D', true); // true
validateIso8601Duration('P1.5Y', true); // false (no decimals in strict mode)
// Form validation
if (!validateIso8601Duration($_POST['duration'])) {
    throw new Exception('Invalid duration format');
}
link

ISO 8601 Duration specification

PHP DateInterval constructor

author

Marc Alcaraz (ekameleon)

since
1.0.1
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
example
isIso8601Interval('2026-05-14T00:00:00Z/2026-05-15T00:00:00Z'); // true
isIso8601Interval('2026-05-14T00:00:00Z/P1D');                  // true
isIso8601Interval('P1D/2026-05-15T00:00:00Z');                  // true
isIso8601Interval('P1D');                                        // false (use Iso8601Duration)
isIso8601Interval('P1D/P2D');                                    // false (two durations)
isIso8601Interval('--/2026-05-15T00:00:00Z');                    // false (open interval)
link

ISO 8601 Time intervals

author

Marc Alcaraz (ekameleon)

since
1.0.2
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:

  • R is the recurrence designator (required)
  • n is 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
example
isIso8601Recurrence('R/2026-05-14T00:00:00Z/P1D');     // true (infinite)
isIso8601Recurrence('R5/2026-05-14T00:00:00Z/P1D');    // true (5 occurrences)
isIso8601Recurrence('R0/2026-05-14T00:00:00Z/PT0S');   // true (zero, degenerate)
isIso8601Recurrence('R10/P1D/2026-05-15T00:00:00Z');   // true
isIso8601Recurrence('2026-05-14T00:00:00Z/P1D');       // false (missing R)
isIso8601Recurrence('R-1/P1D/2026-05-15T00:00:00Z');   // false (negative count)
isIso8601Recurrence('R/P1D');                          // false (interval must be bounded)
link

ISO 8601 Repeating intervals

author

Marc Alcaraz (ekameleon)

since
1.0.2
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
example
isIso8601Time('T14:30:00Z');       // true
isIso8601Time('T08:15:30+02:00');  // true
isIso8601Time('T23:59:59');        // true
isIso8601Time('14:30:00');         // false (missing T)
isIso8601Time('INVALID');           // false
// Strict mode validation
isIso8601Time('T14:30:00.123Z', true); // true (fractional seconds allowed)
isIso8601Time('T14:60:00', true);      // false (invalid minutes)
link

ISO 8601 Time specification

PHP DateTimeImmutable documentation

author

Marc Alcaraz (ekameleon)

since
1.0.1
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
example
use org\iso\helpers\toIso8601Date;
use DateTimeImmutable;

$dt = new DateTimeImmutable('2026-05-14 08:15:30');
echo toIso8601Date($dt);         // "2026-05-14"
echo toIso8601Date($dt, true);   // "20260514"
link

ISO 8601 calendar dates

author

Marc Alcaraz (ekameleon)

since
1.0.2
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...]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

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
example
// Create a duration of 1 year, 2 months, and 3 days
$interval = new DateInterval('P1Y2M3D');
echo toIso8601Duration($interval); // Output: "P1Y2M3D"
// Create a duration of 4 hours and 30 minutes
$interval = new DateInterval('PT4H30M');
echo toIso8601Duration($interval); // Output: "PT4H30M"
// Create a complex duration
$interval = new DateInterval('P2Y3M15DT12H45M30S');
echo toIso8601Duration($interval); // Output: "P2Y3M15DT12H45M30S"
// Create an interval from date difference
$start = new DateTime('2024-01-01');
$end = new DateTime('2024-12-31');
$interval = $start->diff($end);
echo toIso8601Duration($interval); // Output: "P11M30D"
// Zero duration
$interval = new DateInterval('PT0S');
echo toIso8601Duration($interval); // Output: "P0D"
link

ISO 8601 Duration specification

PHP DateInterval documentation

author

Marc Alcaraz (ekameleon)

since
1.0.1
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
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