Skip to content

Core\Time\Instant::fromEpoch

Core\Time\Instant::fromEpoch(
    int $seconds,
    {nanoseconds?: uint},
): Core\Time\Instant

Builds an Instant from a Unix timestamp. A Unix timestamp is the number of seconds since 1 January 1970 at midnight in UTC.

A negative number of seconds is a time before 1970. The nanoseconds option adds nanoseconds after the second. So -1 with { nanoseconds: 500000000 } is half a second after -1, not before it.

The seconds must be between -377705023201 and 253402207200. That is about the years -9999 to 9999. A number outside that range throws a RuntimeError, and so does a nanoseconds value of one second or more.

The examples below show a timestamp from a database as a local time, a time before 1970, and a check of whether a login token has expired.

Parameters

  • $secondsintWhole seconds since 1970-01-01T00:00:00Z. A time before that is negative.

Options

  • nanosecondsuintoptionalNanoseconds to add after the second, from 0 to 999999999. The default is 0.

Return value

Returns Core\Time\Instant. The Instant.

Errors

  • RuntimeErrornanoseconds is 1000000000 or more. Or $seconds is not from -377705023201 to 253402207200. These are the times -9999-01-02T01:59:59Z and 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.

Examples

A timestamp from a database

<?nvs
// Shows how to turn a Unix timestamp, such as one stored in a database, into a date and time.
// 1715688000 is the number of seconds since 1 January 1970 in UTC.
var $created = Core\Time\Instant::fromEpoch(1715688000);
// `toIso` prints the moment in UTC. This prints 2024-05-14T12:00:00Z.
echo $created->toIso(), "\n";
// `in` shows the same moment in a time zone. This prints 14.05.2024 14:00 for Berlin.
echo $created->in('Europe/Berlin')->format('dd.MM.yyyy HH:mm'), "\n";
Output
2024-05-14T12:00:00Z
14.05.2024 14:00

A time before 1970

<?nvs
// Shows how a negative timestamp gives a time before 1970, and how `nanoseconds` adds part of a second.
// The first Moon landing was 14182940 seconds before 1970. This prints 1969-07-20T20:17:40Z.
echo Core\Time\Instant::fromEpoch(-14182940)->toIso(), "\n";
// `nanoseconds` always moves the time forward, also when the seconds are negative.
// -1 is 23:59:59 on the last day of 1969. Half a second later is 23:59:59.5.
echo Core\Time\Instant::fromEpoch(-1, { nanoseconds: 500000000 })->toIso(), "\n";
Output
1969-07-20T20:17:40Z
1969-12-31T23:59:59.5Z

Has a login token expired

<?nvs
// Shows how to check whether a login token has expired, from the Unix timestamps written inside it.
// A token often contains the time it was issued (`iat`) and the time it expires (`exp`).
int $iat = 1715688000;
int $exp = 1715691600;
var $issued = Core\Time\Instant::fromEpoch($iat);
var $expires = Core\Time\Instant::fromEpoch($exp);
// `since` gives the time between two instants. This prints 3600, because the token is valid for one hour.
echo $expires->since($issued)->toSeconds(), "\n";
// In a real program `$now` is `Core\Time::now()`. Fixed times make the output the same on every run.
array<int> $checkedAt = [1715689800, 1715695200];
foreach ($checkedAt as int $seconds) {
var $now = Core\Time\Instant::fromEpoch($seconds);
// `compareTo` returns a number above 0 when `$now` is later than `$expires`.
echo $now->toIso(), ': ', $now->compareTo($expires) > 0 ? 'expired' : 'valid', "\n";
}
Output
3600
2024-05-14T12:30:00Z: valid
2024-05-14T14:00:00Z: expired