Core\Time\DateTime::fromIso
Core\Time\DateTime::fromIso(
string $text,
?Core\Time\Zone $zone = null,
): Core\Time\DateTimeReads a date and time written as text, such as 2024-06-01 09:30, and returns a DateTime.
The text is a date, YYYY-MM-DD, and can have a time after a T or one space. The time is HH:MM, HH:MM:SS or HH:MM:SS.f. A date alone gives the start of that day. A local time that the clocks skip or repeat is handled as in Core\Time\DateTime::at.
The text can end in an offset and a zone name in brackets, such as 2024-06-01T09:30:00+02:00[Europe/Berlin]. This is the text that toIso writes. For this text, fromIso needs no second argument. A text with no zone name needs the time zone as the second argument.
A text with an offset and no zone name, such as +02:00 or Z alone, throws a ParseError. For that text, use Core\Time\Instant::fromIso($text)->in($zone). A date that does not exist also throws a ParseError.
Write a fixed time zone as a string, such as 'Europe/Berlin'. For a zone name read while the program runs, pass Core\Time\Zone::of($name).
The examples below show a meeting time and a date alone, texts with an offset, and one local time in several time zones.
Parameters
$textstringYYYY-MM-DD, or a date,Tor one space, and a time. The time isHH:MM,HH:MM:SSorHH:MM:SSwith one to nine digits after a.. After the time, the text may have an offset such as+01:00and then a zone name in brackets, such as[Europe/Vienna]. An offset needs the zone after it.$zone?Core\Time\Zonedefault nullThe zone the date and the time are read in. It is needed only when$texthas no zone in brackets. If both name a zone, they must be the same zone.
Return value
Returns Core\Time\DateTime. The DateTime in the zone of the text, or in $zone. Without an offset, a time the zone skips moves forward past the gap, and a time that happens twice is the earlier one. The offset shows which of the two times is meant.
Errors
ParseError$textis not in this form, or it is not a real date or time, such as2024-02-30. It is also thrown when the zone does not have the offset at that time. It is thrown when$texthas no zone and$zoneisnull, and when the two name different zones. A text with aZ, or an offset and no zone, also throws it. Read such a text withInstant::fromIso, then call->in($zone). It is also thrown for a time outside-9999-01-02T01:59:59Z..=9999-12-30T22:00:00Z.
Changelog
| Version | Change |
|---|---|
0.0.5 | Core\Time\DateTime::fromIso reads a local date and time, such as 2024-03-01 09:30, in the zone you give. Core\Time\DateTime::at builds a DateTime from a Date, a TimeOfDay and a zone. Core\Time\DateTime::parse reads text in a pattern you choose. |
0.0.6 | Core\Time\DateTime::fromIso reads a text with an offset and a time zone name, such as 2026-01-05T08:30:00+01:00[Europe/Vienna]. For such a text, the $zone argument is optional. An offset that the zone does not have at that time throws a ParseError. |
Examples
A meeting time and a date alone
<?nvs// Shows how to read a local date and time, and a date alone, in a time zone.
// The meeting is at 09:30 in Berlin. In June, Berlin is two hours ahead of UTC.// This prints 2024-06-01 09:30 +02:00.var $meeting = Core\Time\DateTime::fromIso('2024-06-01 09:30', 'Europe/Berlin');echo $meeting->format('yyyy-MM-dd HH:mm XXX'), "\n";
// A date alone is the start of that day. This prints 2024-06-01 00:00 +02:00.var $day = Core\Time\DateTime::fromIso('2024-06-01', 'Europe/Berlin');echo $day->format('yyyy-MM-dd HH:mm XXX'), "\n";2024-06-01 09:30 +02:002024-06-01 00:00 +02:00Texts with an offset
<?nvs// Shows how `fromIso` reads a text with an offset from UTC.
// This text has an offset and a zone name in brackets. This is the text `toIso` writes.// `fromIso` needs no second argument for it. This prints 2024-06-01 09:30 +02:00.var $saved = Core\Time\DateTime::fromIso('2024-06-01T09:30:00+02:00[Europe/Berlin]');echo $saved->format('yyyy-MM-dd HH:mm XXX'), "\n";
// A text that ends in `Z` or in an offset alone has no zone name.// `fromIso` throws a `ParseError` for it.array<string> $inputs = ['2024-06-01T07:30:00Z', '2024-06-01T09:30:00+02:00'];foreach ($inputs as string $text) { try { echo Core\Time\DateTime::fromIso($text, 'Europe/Berlin')->format('yyyy-MM-dd HH:mm XXX'), "\n"; } catch (ParseError $error) { echo 'no zone name: ', $text, "\n"; }}
// `Core\Time\Instant::fromIso` reads such a text. `in` gives the time in Berlin.// This prints 2024-06-01 09:30 +02:00.var $berlin = Core\Time\Zone::of('Europe/Berlin');echo Core\Time\Instant::fromIso('2024-06-01T07:30:00Z')->in($berlin)->format('yyyy-MM-dd HH:mm XXX'), "\n";2024-06-01 09:30 +02:00no zone name: 2024-06-01T07:30:00Zno zone name: 2024-06-01T09:30:00+02:002024-06-01 09:30 +02:00A reminder in each users time zone
<?nvs// Shows how to read one local time in the time zone each user has saved.
// Each user wants a reminder at 08:00 on their own clock.// `Core\Time\Zone::of` reads the zone name, because the names are only known when the program runs.array<string> $zones = ['Europe/Berlin', 'America/New_York', 'Asia/Tokyo'];foreach ($zones as string $name) { var $reminder = Core\Time\DateTime::fromIso('2024-06-01T08:00', Core\Time\Zone::of($name));
// `toInstant` gives the moment in UTC, so the three reminders are sent at different times. echo $name, ': ', $reminder->toInstant()->toIso(), "\n";}Europe/Berlin: 2024-06-01T06:00:00ZAmerica/New_York: 2024-06-01T12:00:00ZAsia/Tokyo: 2024-05-31T23:00:00Z