Skip to content

Core\Time\Instant::in

Shows this instant on the calendar and clock of a time zone, and returns it as a DateTime.

An instant is one moment, and it has no time zone. To get a date, an hour or a weekday, you need a zone, and in is the way to add one. in(Core\Time\Zone::of("Europe/Berlin")) returns the date and time a clock in Berlin shows at that moment.

The zone uses the offset it has at that moment. The same zone can give +01:00 in winter and +02:00 in summer. The instant itself does not change, so the DateTime and the instant are always the same moment.

The examples show one moment in three cities, the change to summer time, and order times shown in each customer's own zone.

Parameters

  • $zoneCore\Time\ZoneThe time zone to show the instant in. Its offset at this instant is used, including summer time.

Return value

Returns Core\Time\DateTime. A DateTime at the same instant, in $zone. The instant does not change.

Changelog

VersionChange
0.0.5Core\Time\Instant::in takes a zone written as text, such as $instant->in('Europe/Berlin') or $instant->in('+05:30'). The name is checked when the program compiles, and a wrong name does not compile. Every other method that takes a zone accepts text in the same way.

Examples

The same moment in three cities

<?nvs
// Shows one moment on the clocks of three cities.
Core\Time\Instant $call = Core\Time\Instant::fromIso('2024-03-01T15:00:00Z');
array<string> $cities = ['Europe/London', 'Europe/Berlin', 'America/New_York'];
foreach ($cities as string $city) {
// `in` returns a `DateTime`: the same moment, on this zone's calendar and clock.
Core\Time\DateTime $local = $call->in(Core\Time\Zone::of($city));
echo $city, ': ', $local->format('HH:mm'), "\n";
}
// This prints 15:00, 16:00 and 10:00.
Output
Europe/London: 15:00
Europe/Berlin: 16:00
America/New_York: 10:00

Summer time moves the clock

<?nvs
// Shows that `in` uses the offset the zone has at that moment, including summer time.
Core\Time\Zone $berlin = Core\Time\Zone::of('Europe/Berlin');
// Both instants are at 12:00 UTC. Berlin is one hour ahead in winter and two hours ahead in summer.
Core\Time\Instant $winter = Core\Time\Instant::fromIso('2024-01-15T12:00:00Z');
Core\Time\Instant $summer = Core\Time\Instant::fromIso('2024-07-15T12:00:00Z');
// This prints 13:00 +01:00 and then 14:00 +02:00.
echo $winter->in($berlin)->format('HH:mm XXX'), "\n";
echo $summer->in($berlin)->format('HH:mm XXX'), "\n";
Output
13:00 +01:00
14:00 +02:00

An order time in the customers zone

<?nvs
// Shows when each order was placed, in the time zone of the customer who placed it.
// The shop saves every order time in UTC. Each customer has a time zone in their profile.
array<string> $placed = [
'2024-03-01T23:30:00Z' => 'Europe/Vienna',
'2024-03-02T01:15:00Z' => 'America/Los_Angeles',
'2024-03-02T08:00:00Z' => 'Asia/Tokyo',
];
foreach ($placed as string $at => string $zone) {
Core\Time\DateTime $local = Core\Time\Instant::fromIso($at)->in(Core\Time\Zone::of($zone));
echo 'Ordered on ', $local->format("d MMMM yyyy 'at' HH:mm"), ' (', $zone, ")\n";
}
// The first order is on 2 March in Vienna. It is still 1 March in UTC.
Output
Ordered on 2 March 2024 at 00:30 (Europe/Vienna)
Ordered on 1 March 2024 at 17:15 (America/Los_Angeles)
Ordered on 2 March 2024 at 17:00 (Asia/Tokyo)