Php convert timezone 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.

asked Jan 18, 2011 at 19:49

3

you can use the following

format('P');
?>

Php convert timezone to offset

answered Jan 18, 2011 at 19:58

Cecil ZorgCecil Zorg

1,45913 silver badges15 bronze badges

3

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

answered Jan 18, 2011 at 22:33

salathesalathe

50.4k11 gold badges103 silver badges130 bronze badges

1

I like using PHP Carbon:

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

answered Jun 5, 2015 at 2:29

mopo922mopo922

6,2063 gold badges27 silver badges31 bronze badges

❮ PHP Date/Time Reference

Example

Return the timezone offset from GMT:

$tz=timezone_open("Asia/Taipei");
$dateTimeOslo=date_create("now",timezone_open("Europe/Oslo"));
echo timezone_offset_get($tz,$dateTimeOslo);
?>

Try it Yourself »


Definition and Usage

The timezone_offset_get() returns the timezone offset from GMT.


Syntax

timezone_offset_get(object, datetime)

Parameter Values

ParameterDescription
object Required. Specifies a DateTimeZone object returned by timezone_open()
datetime Required. Specifies a date/time to compute the offset from

Technical Details

Return Value:Returns the timezone offset in seconds on success. FALSE on failure
PHP Version:5.2+

❮ PHP Date/Time Reference


This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters

/**
* Get the offset of the given timezone (in seconds)
*/
function getTimeZoneOffset($timezone='') {
$tz_tmp = new DateTimeZone($timezone);
$dt_tmp = new DateTime('now', $tz_tmp);
$offset = $dt_tmp->getOffset();
return $offset;
}
/**
* Convert the given time from the origin time zone to the destination time zone
*/
function convertTime($datetime='0000-00-00 00:00:00', $origin_timezone='GMT', $destination_timezone='GMT', $output_as_mysql=false) {
$time = $datetime;
if (!is_int($time)) {
$time = strtotime($datetime);
}
$originOffset = $this->getTimeZoneOffset($origin_timezone);
$destinationOffset = $this->getTimeZoneOffset($destination_timezone);
$gmtTime = $time - $originOffset;
$convertedTime = $gmtTime + $destinationOffset;
if ($output_as_mysql) {
$convertedTime = date('Y-m-d H:i:s', $convertedTime);
}
return $convertedTime;
}

How to get offset from timezone in PHP?

The timezone_offset_get() function is an inbuilt function in PHP which is used to return the timezone offset from GMT. The date time object and the date-time are sent as a parameter to the timezone_offset_get() function and return the timezone offset in seconds on success or False on failure.

How do you convert timezone to offset?

$tz->getOffset(new \DateTime()); Example. Note that the offset produced in the example will vary between 0 and 3600 depending on what date and time you are viewing it. As you are storing the users timezone you never have to face the futility of trying to convert an offset to the correct timezone.

How to get local timezone in PHP?

The date_default_timezone_get() function returns the default timezone used by all date/time functions in the script.