Skip to content

Core\Time\Instant::fromIso

Reads a date and time written as text, such as 2024-06-01T09:30:00+02:00, and returns an Instant, one exact moment in time. The text must follow ISO 8601, the international standard way to write a date and time.

The text needs a date, T or one space, a time and an offset from UTC. The offset is Z for UTC, or a value such as +02:00. Part of a second, such as .25, is kept. For a time without an offset, use Core\Time\DateTime::fromIso with a time zone.

These texts throw a ParseError:

  • a text without an offset
  • a lower-case t or z
  • a , before the part of a second
  • the second 60
  • an offset without a colon, such as +0100
  • a year that does not have four digits, such as -009999
  • a date that does not exist, such as 2024-02-30
  • a time before -9999-01-02T01:59:59Z or after 9999-12-30T22:00:00Z

The error message names the wrong part. A text written in the code is checked when the program compiles.

The examples below show a time with an offset and texts that throw. The last one shows how long a job ran.

Parameters

  • $textstringThe timestamp text, with a date, T or one space, a time and a Z or ±HH:MM offset. T and Z are upper case. A part of a second follows a ..

Return value

Returns Core\Time\Instant. The Instant the text names.

Errors

  • ParseError$text is not such a timestamp. The error names the part of the text where reading stopped. A lower-case t or z throws, and so does a , before the part of a second. The second 60 and an offset without a colon, such as +0100, also throw. It is also thrown for a time before -9999-01-02T01:59:59Z or after 9999-12-30T22:00:00Z.

Changelog

VersionChange
0.0.5Core\Time\Instant::fromIso reads a timestamp such as 2024-03-01T12:00:00Z and returns an Instant. Core\Time\Instant::fromEpoch builds an Instant from the number of seconds since 1 January 1970.
0.0.6Instant::fromIso reads an upper-case T and Z, a . before the fraction, and an offset with a colon, such as +02:00. A lower-case t or z, a comma, the second 60 or an offset like +0100 throws a ParseError. So does a year that does not have four digits, such as -009999.

Examples

A time with an offset

<?nvs
// Shows how to read a date and time that is written with an offset from UTC.
// `+02:00` means the clock was two hours ahead of UTC. `toIso` prints the moment in UTC.
// This prints 2024-06-01T07:30:00Z.
var $local = Core\Time\Instant::fromIso('2024-06-01T09:30:00+02:00');
echo $local->toIso(), "\n";
// The same moment written in UTC is an equal instant, so `compareTo` returns 0.
var $utc = Core\Time\Instant::fromIso('2024-06-01T07:30:00Z');
echo $local->compareTo($utc), "\n";
Output
2024-06-01T07:30:00Z
0

Texts that are not timestamps

<?nvs
// Shows what happens when a text is not a complete, valid timestamp.
// The second text has no offset. The third has a day that does not exist.
// Both throw a `ParseError`, and its message says which part is wrong.
array<string> $inputs = ['2024-06-01T09:30:00Z', '2024-06-01T09:30:00', '2024-02-30T09:30:00Z'];
foreach ($inputs as string $text) {
try {
echo Core\Time\Instant::fromIso($text)->toIso(), "\n";
} catch (ParseError $error) {
echo 'not a timestamp: ', $text, "\n";
}
}
Output
2024-06-01T09:30:00Z
not a timestamp: 2024-06-01T09:30:00
not a timestamp: 2024-02-30T09:30:00Z

How long a job ran

<?nvs
// Shows how to find how long a job ran, from the start and end times a web service returned.
// Web services often send times as ISO 8601 text. These two use different offsets.
string $started = '2024-06-01T09:30:00.250+02:00';
string $finished = '2024-06-01T07:42:10Z';
// `since` gives the time between two instants. The job ran for 12 minutes and 9.75 seconds.
// `toSeconds` counts only the whole seconds, so this prints 729.
var $ran = Core\Time\Instant::fromIso($finished)->since(Core\Time\Instant::fromIso($started));
echo $ran->toSeconds(), "\n";
Output
729