Skip to content

Core\Time\DateTime::zone

$dateTime->zone(): Core\Time\Zone

Returns the time zone of a date and time, as a Core\Time\Zone.

Every date and time is in one time zone. It is the zone you gave when you created the value, or the zone you converted a moment to. zone returns it, so you can use it again for another value.

A common use is to show other times in the same zone. For a booking in Sydney, take the zone of the booking and convert each reminder time to it.

Good to know: a zone with a place name, such as Europe/Berlin, follows the clock changes of that place. A zone made from a fixed offset, such as +05:30, has the same offset all year.

Return value

Returns Core\Time\Zone. The Zone component.

Examples

The time zone of a date and time

<?nvs
// Shows how to get the time zone of a date and time.
var $meeting = Core\Time\DateTime::fromIso('2024-03-05 14:30', 'Europe/Berlin');
// `zone` returns the `Core\Time\Zone` the value was made in.
// The `VV` pattern prints the name of the zone. This prints Europe/Berlin.
var $zone = $meeting->zone();
echo $meeting->format('VV'), "\n";
// The zone knows its offset from UTC at each moment.
// This prints 1h in March and 2h in July, because Berlin changes its clocks in summer.
echo $zone->offsetAt($meeting->toInstant())->toString(), "\n";
echo $zone->offsetAt(Core\Time\Instant::fromIso('2024-07-01T12:00:00Z'))->toString(), "\n";
Output
Europe/Berlin
1h
2h

A zone with a fixed offset

<?nvs
// Shows the time zone of a value that was read at a fixed offset from UTC.
// `Zone::fixed` makes a zone that is always 5 hours and 30 minutes ahead of UTC.
var $reading = Core\Time\Instant::fromIso('2024-03-05T10:00:00Z')->in(Core\Time\Zone::fixed(5h30m));
// A fixed zone has no place name, so its name is the offset. This prints +05:30.
echo $reading->format('VV'), "\n";
// The offset is the same all year. This prints 5h30m.
var $zone = $reading->zone();
echo $zone->offsetAt(Core\Time\Instant::fromIso('2024-07-01T00:00:00Z'))->toString(), "\n";
Output
+05:30
5h30m

Show reminders in the users time zone

<?nvs
// Shows how to print several saved times in the time zone of one user's booking.
// The user booked a table in Sydney, so the booking is in the Sydney time zone.
var $booking = Core\Time\DateTime::fromIso('2024-12-20 19:00', 'Australia/Sydney');
// The server saves each reminder time in UTC.
array<string> $reminders = ['2024-12-19T08:00:00Z', '2024-12-20T06:00:00Z'];
// `in($booking->zone())` shows each reminder in the same zone as the booking.
// Sydney is 11 hours ahead of UTC in December. This prints 19 December 19:00 and 20 December 17:00.
foreach ($reminders as string $text) {
var $local = Core\Time\Instant::fromIso($text)->in($booking->zone());
echo 'Reminder: ', $local->format('d MMMM HH:mm'), "\n";
}
echo 'Table: ', $booking->format('d MMMM HH:mm VV'), "\n";
Output
Reminder: 19 December 19:00
Reminder: 20 December 17:00
Table: 20 December 19:00 Australia/Sydney