Skip to content

Core\Time\Instant::compareTo

$instant->compareTo(Core\Time\Instant $other): int$instant->compareTo(
    Core\Time\Instant $other,
): int

Compares this instant with another one and returns -1, 0 or 1.

The result is -1 when this instant is earlier, 0 when both are the same moment, and 1 when this instant is later. An instant is one exact point in time, so the time zone it was written in does not matter. 2024-03-01T13:00:00+01:00 and 2024-03-01T12:00:00Z are the same moment.

You can also write <, >, <=, >= and <=> between two instants. They all use compareTo, so they always give the same answer.

Good to know: == compares two instants by their moment too. It is true for the two texts above.

Parameters

  • $otherCore\Time\InstantThe instant to compare against.

Return value

Returns int. -1 when this instant is earlier than $other, 0 when they are the same instant, 1 when it is later.

Examples

Earlier same later

<?nvs
// Shows the three results of `compareTo` for an earlier, an equal and a later instant.
Core\Time\Instant $noon = Core\Time\Instant::fromIso('2024-03-01T12:00:00Z');
// This prints -1, because 11 o'clock is earlier than noon.
echo Core\Time\Instant::fromIso('2024-03-01T11:00:00Z')->compareTo($noon), "\n";
// This prints 0. The epoch second 1709294400 is noon on 1 March 2024.
echo Core\Time\Instant::fromEpoch(1709294400)->compareTo($noon), "\n";
// This prints 1. One nanosecond after noon is already later.
echo Core\Time\Instant::fromEpoch(1709294400, { nanoseconds: 1 })->compareTo($noon), "\n";
Output
-1
0
1

The same moment in two zones

<?nvs
// Shows how to test whether two timestamps written in different time zones are the same moment.
Core\Time\Instant $london = Core\Time\Instant::fromIso('2024-03-01T12:00:00Z');
Core\Time\Instant $vienna = Core\Time\Instant::fromIso('2024-03-01T13:00:00+01:00');
// `==` compares the moment. Both texts name the same moment, so this prints "same".
echo $london == $vienna ? 'same' : 'different', "\n";
// `compareTo` returns 0 for the same moment. This prints 0.
echo $london->compareTo($vienna), "\n";
// `<` uses `compareTo` too. This prints "no".
echo $london < $vienna ? 'yes' : 'no', "\n";
Output
same
0
no

Has the link expired

<?nvs
// Checks which password reset links have expired. Each link is valid for 30 minutes.
// A real program uses `Core\Time::now()` here. A fixed time keeps the output the same.
Core\Time\Instant $now = Core\Time\Instant::fromIso('2024-03-01T12:00:00Z');
array<string> $sent = [
'anna' => '2024-03-01T11:45:00Z',
'ben' => '2024-03-01T11:30:00Z',
'cleo' => '2024-03-01T09:10:00Z',
];
foreach ($sent as string $user => string $at) {
Core\Time\Instant $expires = Core\Time\Instant::fromIso($at)->plus(30m);
// `compareTo` returns -1 while `$now` is still earlier than the expiry time.
if ($now->compareTo($expires) < 0) {
echo $user, ": valid\n";
} else {
echo $user, ": expired\n";
}
}
// This prints "valid" for anna. The link for ben expires at exactly 12:00, so it is expired.
Output
anna: valid
ben: expired
cleo: expired