Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Timezone Name to Offset

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.

like image 638
Andrew M Avatar asked Sep 05 '25 03:09

Andrew M


2 Answers

you can use the following

<?php

$timezone = 'Pacific/Nauru';
$time = new \DateTime('now', new DateTimeZone($timezone));
$timezoneOffset = $time->format('P');
?>
like image 80
Cecil Zorg Avatar answered Sep 07 '25 23:09

Cecil Zorg


Another option would be to use the available DateTimeZone::getOffset() method, which returns the offset in seconds for the time zone at the specified date/time.

$timezone = new DateTimeZone("Europe/Andorra");
$offset   = $timezone->getOffset(new DateTime);
like image 43
salathe Avatar answered Sep 07 '25 21:09

salathe