Core\Time\DateTime::compareTo
$dateTime->compareTo(Core\Time\DateTime $other): int$dateTime->compareTo( Core\Time\DateTime $other, ): int
Checks which of two date-times is earlier, and returns -1, 0 or 1.
The result is -1 when this date-time is earlier than the other one, 0 when both are the same moment, and 1 when it is later. The check uses the moment, down to the nanosecond. The time zone does not count. 10:00 in Europe/Berlin and 09:00 in Europe/London on the same winter day are the same moment, so the result is 0.
The operators <, >, <=, >= and <=> use compareTo too, so you can write $a < $b. == also compares the moment, so it is true for the two values above.
Good to know: two values can be the same moment and still show different clock times. To check that two values also have the same time zone, compare zone() as well.
Parameters
$otherCore\Time\DateTimeThe date-time to compare against.
Return value
Returns int. -1 when this date-time is earlier than $other, 0 when both are the same moment, 1 when it is later.
Changelog
| Version | Change |
|---|---|
0.0.6 | Core\Time\DateTime::compareTo returns -1, 0 or 1, by the moment of each value. So <, <=, > and >= now work on two DateTime values, also in different time zones. |
Examples
Earlier or later
<?nvs// Shows the three results of `compareTo` for two date-times.
var $start = Core\Time\DateTime::fromIso('2026-03-02 09:00', 'Europe/Berlin');var $end = Core\Time\DateTime::fromIso('2026-03-02 17:30', 'Europe/Berlin');
// This prints -1, then 1, then 0.echo $start->compareTo($end), "\n";echo $end->compareTo($start), "\n";echo $start->compareTo($start), "\n";
// `<` uses `compareTo`. This prints "start first".if ($start < $end) { echo "start first\n";}-110start firstThe same moment in two zones
<?nvs// Shows that `compareTo` compares the moment, and not the time on the clock.
// In January, Berlin is one hour ahead of London. 10:00 in Berlin is 09:00 in London.var $berlin = Core\Time\DateTime::fromIso('2026-01-05 10:00', 'Europe/Berlin');var $london = Core\Time\DateTime::fromIso('2026-01-05 09:00', 'Europe/London');
// Both values are the same moment, so this prints 0.echo $berlin->compareTo($london), "\n";
// `==` compares the moment too. This prints "same moment".echo $berlin == $london ? 'same moment' : 'other moment', "\n";
// 09:30 in London is later, even though the clock shows an earlier hour. This prints 1.echo Core\Time\DateTime::fromIso('2026-01-05 09:30', 'Europe/London')->compareTo($berlin), "\n";0same moment1Sorting flights by departure
<?nvs// Sorts flights that leave from airports in different time zones by their departure.
array<Core\Time\DateTime> $departures = [ Core\Time\DateTime::fromIso('2026-05-10 14:00', 'America/New_York'), Core\Time\DateTime::fromIso('2026-05-10 09:00', 'Europe/Paris'), Core\Time\DateTime::fromIso('2026-05-10 19:00', 'Asia/Tokyo'),];
// `Core\Arr::sort` uses `compareTo` to put the earliest departure first. In UTC, the flights// leave at 07:00 from Paris, 10:00 from Tokyo and 18:00 from New York, in this order.array<Core\Time\DateTime> $sorted = Core\Arr::sort($departures, { comparator: fn(Core\Time\DateTime $a, Core\Time\DateTime $b): int => $a->compareTo($b),});foreach ($sorted as Core\Time\DateTime $departure) { echo $departure->toIso(), "\n";}2026-05-10T09:00:00+02:00[Europe/Paris]2026-05-10T19:00:00+09:00[Asia/Tokyo]2026-05-10T14:00:00-04:00[America/New_York]