Skip to content

Core\Time\DateTime::toIso

$dateTime->toIso(): string

Returns the date and time as ISO 8601 text, with its offset from UTC and its time zone, such as 2026-01-05T08:30:00+01:00[Europe/Vienna].

The text has the local date and time, then the offset, then the zone name in brackets. It always has seconds. It has a fraction of a second only when the fraction is not zero, such as 09:30:15.25.

Core\Time\DateTime::fromIso reads this text back, with no second argument. The result is the same moment in the same time zone. When the clocks go back, a local time happens twice. The offset in the text shows which of the two times it is.

Use toIso to save a DateTime in a file or a database column, or to send it to another program. For text a person reads, use format.

Return value

Returns string. A string with the local date and time, the offset from UTC and the zone name in brackets. It always has seconds. It has a fraction of a second only when the fraction is not zero.

Changelog

VersionChange
0.0.6toIso() writes a Date, TimeOfDay or DateTime as ISO 8601 text, such as 2026-01-05 or 08:30:00. A DateTime also writes its offset and time zone: 2026-01-05T08:30:00+01:00[Europe/Vienna]. fromIso reads each text back to an equal value.

Examples

Writing a date time as text

<?nvs
// Shows the text that `toIso` writes for a date and time in a time zone.
// The text has the local date and time, the offset from UTC and the zone name.
// This prints 2026-01-05T08:30:00+01:00[Europe/Vienna].
var $meeting = Core\Time\DateTime::fromIso('2026-01-05 08:30', 'Europe/Vienna');
echo $meeting->toIso(), "\n";
// In July, Vienna is two hours ahead of UTC. This prints 2026-07-05T08:30:00+02:00[Europe/Vienna].
echo $meeting->plus(6, Core\Unit::Month)->toIso(), "\n";
// The same moment in UTC has the offset +00:00. This prints 2026-01-05T07:30:00+00:00[UTC].
echo $meeting->toInstant()->in(Core\Time\Zone::UTC)->toIso(), "\n";
Output
2026-01-05T08:30:00+01:00[Europe/Vienna]
2026-07-05T08:30:00+02:00[Europe/Vienna]
2026-01-05T07:30:00+00:00[UTC]

Seconds and fractions

<?nvs
// Shows when the text from `toIso` has seconds and a fraction of a second.
// The text always has seconds, even when they are zero. This prints 2024-03-01T09:30:00+00:00[UTC].
echo Core\Time\DateTime::fromIso('2024-03-01 09:30', 'UTC')->toIso(), "\n";
// A fraction of a second is written only when it is not zero.
// This prints 2024-03-01T09:30:15.25+00:00[UTC].
echo Core\Time\DateTime::fromIso('2024-03-01 09:30:15.250', 'UTC')->toIso(), "\n";
Output
2024-03-01T09:30:00+00:00[UTC]
2024-03-01T09:30:15.25+00:00[UTC]

Saving an appointment

<?nvs
// Shows how to save a date and time as text and read the same value back.
// On 27 October 2024, the clocks in Berlin go back one hour, so 02:30 happens twice.
// The first 02:30 has the offset +02:00 and the second has +01:00.
var $first = Core\Time\DateTime::fromIso('2024-10-27 02:30', 'Europe/Berlin');
var $second = $first->toInstant()->plus(1h)->in(Core\Time\Zone::of('Europe/Berlin'));
// `toIso` writes the offset, so the text names one exact moment.
// This prints 2024-10-27T02:30:00+01:00[Europe/Berlin].
string $saved = $second->toIso();
echo $saved, "\n";
// `fromIso` reads the text back with no second argument.
// The value is the second 02:30, not the first. This prints "same moment", then "other moment".
var $loaded = Core\Time\DateTime::fromIso($saved);
echo $loaded == $second ? 'same moment' : 'other moment', "\n";
echo $loaded == $first ? 'same moment' : 'other moment', "\n";
Output
2024-10-27T02:30:00+01:00[Europe/Berlin]
same moment
other moment