Skip to content

Core\Time\Zone

A time zone, such as Europe/Berlin or a fixed offset like +02:00. Every conversion between an Instant and a DateTime needs one, and there is no default zone. Zone::of finds a zone by its name, Zone::fixed makes one from an offset, and offsetAt returns the offset at one instant.

A Zone turns an Instant into a date and a time of day, and back. A zone that is fixed in the code is written as text where a Zone is expected: $instant->in('Europe/Berlin'). The name is checked when the program compiles, and the zone is built once. UTC and an offset such as '+05:30' are written the same way.

Zone::of($name) looks up a name that arrives while the program runs, such as a user's setting. It throws an error for a name the time zone database does not have. Zone::fixed is a constant offset with no daylight saving rules. There is no default zone: Zone::system() returns the zone of the host, and the program passes it on. A zone with daylight saving time has no single offset, so offsetAt needs the instant.

<?nvs
var $berlin = Core\Time\Zone::of('Europe/Berlin');
var $jan = Core\Time\Instant::fromIso('2024-01-15T12:00:00Z');
var $jul = Core\Time\Instant::fromIso('2024-07-15T12:00:00Z');
echo $berlin->offsetAt($jan), "\n";
echo $berlin->offsetAt($jul), "\n";
echo $jul->in('Europe/Berlin')->format('HH:mm xxx VV'), "\n";
var $india = Core\Time\Zone::fixed(5h30m);
echo $jul->in($india)->format('HH:mm xxx'), "\n";
echo Core\Time\Zone::UTC->offsetAt($jul)->toSeconds(), "\n";
echo Core\Time\DateTime::fromIso('2024-07-15 14:00', $berlin)->toInstant()->toIso(), "\n";
string $saved = 'Mars/Olympus';
try {
Core\Time\Zone::of($saved);
} catch (RuntimeError $unknown) {
echo "not in the database\n";
}
Output
1h
2h
14:00 +02:00 Europe/Berlin
17:30 +05:30
0
2024-07-15T12:00:00Z
not in the database

Returned by

You get a Core\Time\Zone from these methods:

Members

4 members.

MemberSignature
ofof(string $id): Core\Time\Zone
fixedfixed(Core\Time\Duration $offset): Core\Time\Zone
systemsystem(): Core\Time\Zone
offsetAtoffsetAt(Core\Time\Instant $i): Core\Time\Duration

Constants

  • Core\Time\Zone::UTCThe UTC zone — the only zone that is ever a default, and only where a call site writes it explicitly.