Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to retrieve timezone offset from GMT+0 in php?

I am no expert in php. I know timezones are supported in PHP.

For a given timezone TZ supported by PHP, I need to retrieve the offset (i.e., number of hours and minutes to add or substract) to a known UTC (i.e. GMT+0) to get that time in the TZ zone.

How can I achieve this? Ultimately, I need to get those offsets for all supported timezones in PHP. Thanks.

like image 481
Jérôme Verstrynge Avatar asked Feb 04 '12 12:02

Jérôme Verstrynge


People also ask

What is timezone_offset_get () function in PHP?

Last Updated : 16 Aug, 2018 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.

Is it possible to set PHP to GMT timezone?

Unfortunately, PHP requires that you call date_default_timezone_set(). If you set that to GMT, your dates from a database will still be in local time, but date('Z') will return zero. If you set it to the server's timezone, you might as well just hard-code the server's offset from GMT in an include file.

How to get the timezone offset of a date and time?

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. Parameters: This function accepts two parameter as mentioned above and described below:

How to get the UTC offset in PHP?

What's the easiest way to get the UTC offset in PHP, relative to the current (system) timezone? returns the UTC offset in seconds. Thanks. Unfortunately, PHP requires that you call date_default_timezone_set (). If you set that to GMT, your dates from a database will still be in local time, but date ('Z') will return zero.


1 Answers

This will be useful for converting offset values into GMT format without any calculation

<?php

  //target time zone
  $target_time_zone = new DateTimeZone('America/Los_Angeles');

  //find kolkata time 
  $date_time = new DateTime('now', $target_time_zone);

  //get the exact GMT format
  echo 'GMT '.$date_time->format('P');
like image 88
Sundar Avatar answered Sep 28 '22 07:09

Sundar