Core\Time\Zone::offsetAt
$zone->offsetAt(Core\Time\Instant $i): Core\Time\Duration$zone->offsetAt( Core\Time\Instant $i, ): Core\Time\Duration
Returns how far a time zone is from UTC at one instant.
The result is a Duration of whole seconds. It is positive for a zone east of UTC, negative for a zone west of UTC, and zero for UTC itself. For example, Europe/Berlin returns one hour in winter.
You give an instant because many zones have more than one offset. A zone with summer time moves its clocks forward in spring and back in autumn. Europe/Berlin is one hour ahead of UTC in January and two hours ahead in July. The same zone can also have a different offset in the past, when the region changed its rules. A zone made with Zone::fixed has the same offset at every instant.
The examples show a zone in winter and in summer, the second when the clocks change, and the difference in hours between two offices.
Parameters
$iCore\Time\InstantThe instant to read the offset at.
Return value
Returns Core\Time\Duration. The offset as a Duration of whole seconds. It is positive east of UTC, negative west of UTC, and zero for UTC.
Examples
Winter and summer
<?nvs// Shows that a zone with summer time has a different offset in winter and in summer.
Core\Time\Zone $berlin = Core\Time\Zone::of('Europe/Berlin');Core\Time\Instant $january = Core\Time\Instant::fromIso('2024-01-15T12:00:00Z');Core\Time\Instant $july = Core\Time\Instant::fromIso('2024-07-15T12:00:00Z');
// `offsetAt` returns the offset as a `Duration`. `toSeconds` turns it into a number.// One hour is 3600 seconds, so this prints 3600 and 7200.echo $berlin->offsetAt($january)->toSeconds(), "\n";echo $berlin->offsetAt($july)->toSeconds(), "\n";36007200The second the clocks change
<?nvs// Shows the offset of New York one second before and at the moment summer time starts.
Core\Time\Zone $newYork = Core\Time\Zone::of('America/New_York');Core\Time\Instant $change = Core\Time\Instant::fromIso('2024-03-10T07:00:00Z');Core\Time\Instant $before = $change->minus(1s);
// The offset changes at exactly one instant.// This prints -5h and then -4h.echo $newYork->offsetAt($before)->toString(), "\n";echo $newYork->offsetAt($change)->toString(), "\n";-5h-4hHours between two offices
<?nvs// Shows how many hours Tokyo is ahead of London on two days of the year.
Core\Time\Zone $london = Core\Time\Zone::of('Europe/London');Core\Time\Zone $tokyo = Core\Time\Zone::of('Asia/Tokyo');array<string> $days = ['2024-01-10T09:00:00Z', '2024-07-10T09:00:00Z'];
// The difference is the Tokyo offset minus the London offset on that day.// London has summer time and Tokyo does not, so this prints 9h and then 8h.foreach ($days as string $day) { Core\Time\Instant $at = Core\Time\Instant::fromIso($day); Core\Time\Duration $ahead = $tokyo->offsetAt($at)->minus($london->offsetAt($at)); echo $day, ': Tokyo is ', $ahead->toString(), " ahead\n";}2024-01-10T09:00:00Z: Tokyo is 9h ahead2024-07-10T09:00:00Z: Tokyo is 8h ahead