Core\Time\TimeOfDay::fromIso
Core\Time\TimeOfDay::fromIso(string $text): Core\Time\TimeOfDayCore\Time\TimeOfDay::fromIso( string $text, ): Core\Time\TimeOfDay
Reads a time of day written as text, such as 09:30 or 09:30:15.5, and returns a TimeOfDay. The text must follow ISO 8601, the international standard way to write a date and time.
The text is HH:MM, HH:MM:SS or HH:MM:SS with a fraction of a second. The fraction has one to nine digits after the ., so the smallest step is one nanosecond. The hour is 00 to 23, and the minute and the second are 00 to 59. So 24:00 and a second of 60 are not allowed.
A text that does not match throws a ParseError, and the message says which part is wrong. A text written directly in the code is checked when the program compiles, so TimeOfDay::fromIso('25:00') does not compile. A text in a variable is read when the program runs.
The same rules read a TimeOfDay in a route path, a #[Core\Query] value, a command argument and JSON that Core\Json::decodeAs reads.
The examples below show the three forms of the text, texts that are not valid times, and opening hours read from a settings file.
Parameters
$textstringHH:MM,HH:MM:SS, orHH:MM:SSwith one to nine digits after a..
Return value
Returns Core\Time\TimeOfDay. The TimeOfDay.
Errors
ParseError$textis not in this form, or a part is outside its range, such as24:00. The error says which part is wrong.
Changelog
| Version | Change |
|---|---|
0.0.5 | Core\Time\Date::fromIso reads a date such as 2024-03-01. Core\Time\TimeOfDay::fromIso reads a time such as 09:30 or 09:30:15.5. A string literal in either method is checked when the program compiles. |
Examples
Three ways to write a time
<?nvs// Shows the three ways to write a time of day that `fromIso` reads.
// Only hours and minutes. The second is 0, so this prints 09:30:00.000.echo Core\Time\TimeOfDay::fromIso('09:30')->format('HH:mm:ss.SSS'), "\n";
// Hours, minutes and seconds. This prints 09:30:15.000.echo Core\Time\TimeOfDay::fromIso('09:30:15')->format('HH:mm:ss.SSS'), "\n";
// A fraction of a second. `.5` is half a second, so this prints 09:30:15.500.echo Core\Time\TimeOfDay::fromIso('09:30:15.5')->format('HH:mm:ss.SSS'), "\n";
// `nanoseconds` returns the fraction in nanoseconds. This prints 123456789.echo Core\Time\TimeOfDay::fromIso('23:59:59.123456789')->nanoseconds(), "\n";09:30:00.00009:30:15.00009:30:15.500123456789Texts that are not times
<?nvs// Shows what happens when a text is not a valid time of day.
// These texts are read when the program runs. The first one is valid.// Each other text throws a `ParseError`, and its message says which part is wrong.array<string> $inputs = ['18:45', '24:00', '23:59:60', '9:30'];foreach ($inputs as string $text) { try { echo Core\Time\TimeOfDay::fromIso($text)->format('HH:mm'), "\n"; } catch (ParseError $error) { echo $error->message, "\n"; }}18:45Core\Time\TimeOfDay::fromIso(): the hour is `24`, and an hour is `00` to `23`Core\Time\TimeOfDay::fromIso(): the second is `60`, and a second is `00` to `59`Core\Time\TimeOfDay::fromIso(): the hour is 2 digits, in the form `HH:MM:SS`Opening hours from settings
<?nvs// Shows how to read opening hours from settings text and check whether a shop is open.
// Settings files often store times as text like this.array<string> $settings = ['opens' => '08:30', 'closes' => '17:00'];Core\Time\TimeOfDay $opens = Core\Time\TimeOfDay::fromIso($settings['opens']);Core\Time\TimeOfDay $closes = Core\Time\TimeOfDay::fromIso($settings['closes']);
// Times of day compare with `<` and `>=`. This prints "closed", "open" and "closed".array<string> $orders = ['07:55', '12:10:30', '17:00'];foreach ($orders as string $text) { Core\Time\TimeOfDay $at = Core\Time\TimeOfDay::fromIso($text); if ($at >= $opens && $at < $closes) { echo $text, ': open', "\n"; } else { echo $text, ': closed', "\n"; }}07:55: closed12:10:30: open17:00: closed