Oihana PHP Standards

toIso8601Date.php

Table of Contents

Functions

toIso8601Date()  : string
Converts a DateTimeInterface object to its ISO 8601 calendar date string.

Functions

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"

On this page

Search results