Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get timezone offset for a given location

Is it possible in PHP to get the timezone offset for a given location? E.g. when given the location "Sydney/Australia" to get the timezone offset as "+1100". Bonus would be for this function the keep daylight savings in mind (i.e. it's aware of daylight savings and adjusts the offset according).

like image 994
Luke Avatar asked Oct 11 '10 22:10

Luke


People also ask

How do I get a specific timezone offset?

The JavaScript getTimezoneOffset() method is used to find the timezone offset. It returns the timezone difference in minutes, between the UTC and the current local time. If the returned value is positive, local timezone is behind the UTC and if it is negative, the local timezone if ahead of UTC.

How do I get timezone from UTC offset?

Knowing just the offset from UTC, you can't tell what timezone you are in, because of DST. You could consider looking at the time part of the time to try to guess whether DST was in effect then or not, but political considerations make that nearly impossible, as different jurisdictions change the definition of DST.

How do you use timezone offset?

The zone offset can be Z for UTC or it can be a value "+" or "-" from UTC. For example, the value 08:00-08:00 represents 8:00 AM in a time zone 8 hours behind UTC, which is the equivalent of 16:00Z (8:00 plus eight hours). The value 08:00+08:00 represents the opposite increment, or midnight (08:00 minus eight hours).

How do you find time according to time zones?

The . getTimezoneOffset() method should work. This will get the time between your time zone and GMT. You can then calculate to whatever you want.


2 Answers

You can use the DateTimeZone class.

<?php
$timezone = new DateTimeZone("Australia/Sydney");
$offset = $timezone->getOffset(new DateTime("now")); // Offset in seconds
echo ($offset < 0 ? '-' : '+').round($offset/3600).'00'; // prints "+1100"
?>
like image 129
Christian Tellnes Avatar answered Oct 13 '22 01:10

Christian Tellnes


To display a local date/time you can use the following, where 'Europe/Berlin' would be replaced with the user's timezone.

$date = new DateTime($value);
$date->setTimezone(new DateTimeZone('Europe/Berlin'));
echo $date->format('Y-m-d H:i:s');
like image 35
David Snabel-Caunt Avatar answered Oct 13 '22 02:10

David Snabel-Caunt