Core\Time\TimeOfDay::toIso
$timeOfDay->toIso(): stringReturns the time as ISO 8601 text, such as 08:30:00.
The text has the hour, the minute and the second, each with two digits. It has a fraction of a second only when the fraction is not zero, such as 12:00:05.25. Zeros at the end of the fraction are left out.
Core\Time\TimeOfDay::fromIso reads this text back, and the result is the same time. Use toIso to save a time in a file or a database, or to send it to another program. For text a person reads, use format, such as format('h:mm a').
Core\Json::encode, Core\Router::url and Core\Uri::buildQuery write a TimeOfDay as this text. You do not need to call toIso for them.
Return value
Returns string. A string with the hour, the minute and the second. It has a fraction of a second only when the fraction is not zero.
Changelog
| Version | Change |
|---|---|
0.0.6 | toIso() 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 time as text
<?nvs// Shows the text that `toIso` writes for a time of day.
// The text has the hour, the minute and the second, each with two digits.// This prints 08:30:00.echo Core\Time\TimeOfDay::at(8, 30)->toIso(), "\n";
// Midnight is 00:00:00, and the last second of the day is 23:59:59.echo Core\Time\TimeOfDay::at(0, 0)->toIso(), "\n";echo Core\Time\TimeOfDay::at(23, 59, { second: 59 })->toIso(), "\n";08:30:0000:00:0023:59:59A fraction of a second
<?nvs// Shows when the text from `toIso` has a fraction of a second.
// A fraction of a second is written only when it is not zero.// Zeros at the end of the fraction are left out. This prints 12:00:05.25.echo Core\Time\TimeOfDay::fromIso('12:00:05.250')->toIso(), "\n";
// One nanosecond after noon. This prints 12:00:00.000000001.echo Core\Time\TimeOfDay::at(12, 0)->plus(1, Core\Unit::Nanosecond)->toIso(), "\n";
// A time with no fraction has none in the text. This prints 12:00:00.echo Core\Time\TimeOfDay::fromIso('12:00:00.000')->toIso(), "\n";12:00:05.2512:00:00.00000000112:00:00Saving opening hours
<?nvs// Saves the opening hours of a shop as text and reads them back.
Core\Time\TimeOfDay $opens = Core\Time\TimeOfDay::at(9, 0);Core\Time\TimeOfDay $closes = Core\Time\TimeOfDay::at(18, 30);
// `toIso` gives text you can save in a file or a database. This prints 09:00:00-18:30:00.string $saved = $opens->toIso() . '-' . $closes->toIso();echo $saved, "\n";
// `fromIso` reads each time back. The result is the same time, so this prints "same".var $parts = Core\Str::split($saved, '-');var $loaded = Core\Time\TimeOfDay::fromIso($parts[0]);echo $loaded == $opens ? 'same' : 'different', "\n";09:00:00-18:30:00same