PHP Timezone Name to Offset

Andrew M picture Andrew M · Jan 18, 2011 · Viewed 13.7k times · Source

How would I go about converting a timezone name ("Asia/Dubai", "Europe/Andorra", etc) to a GMT offset in hours (or even seconds, since I can convert it from there).

Most people ask for the reverse, but I need to get a timezone offset from the timezone name provided by GeoNames.

Answer

Cecil Zorg picture Cecil Zorg · Jan 18, 2011

you can use the following

<?php

$timezone = 'Pacific/Nauru';
$time = new \DateTime('now', new DateTimeZone($timezone));
$timezoneOffset = $time->format('P');
?>