Andrew M
Andrew M

Reputation: 4288

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.

Upvotes: 29

Views: 18242

Answers (3)

mopo922
mopo922

Reputation: 6381

I like using PHP Carbon:

$offset = Carbon::now('Europe/Andorra')->offsetHours;

Upvotes: 15

MoròSwitie
MoròSwitie

Reputation: 1516

you can use the following

<?php

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

Upvotes: 54

salathe
salathe

Reputation: 51970

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);

Upvotes: 27

Related Questions