简体   繁体   中英

How to set php timezone by offset value in hh:mm, minuts or seconds?

My online application gets users timezone offset in + or - value in minutes. I want to set this value in server.
MySQL / MariaDB accepts timezone values and works perfectly

SET time_zone = "+0:15";

But php only accepts timezone string like this

date_default_timezone_set("Asia/Karachi"); // Works Perfectly

And does not set time in offset value

date_default_timezone_set("+0:15"); // Error

Is there any way to set php time with offset value?

After reading the documentation for the date_default_timezone_set() php function, this seems to be a common issue.

I'm sure there may be alternatives if you choose to use other date functions in php, however, to be able to set the timezone by an offset, you would require a helper function to do this.

I've copied and modified a helper function posted on the php doc page here

function setTimezoneByOffset($offset)
{
  $testTimestamp = time();
    date_default_timezone_set('UTC');
    $testLocaltime = localtime($testTimestamp,true);
    $testHour = $testLocaltime['tm_hour'];       


$abbrarray = timezone_abbreviations_list();
foreach ($abbrarray as $abbr)
{
    //$abbr."<br>";
  foreach ($abbr as $city)
  {
            date_default_timezone_set($city['timezone_id']);
            $testLocaltime     = localtime($testTimestamp,true);
            $hour                     = $testLocaltime['tm_hour'];       
            $testOffset =  $hour - $testHour;
            if($testOffset == $offset)
            {

                return $city['timezone_id'];
            }
  }
}
return false;
}

Where the parameter $offset is a string representation of the offset (ie. '+0:15' ), which returns the timezone id of the timezone which matches that of your input.

My online application gets users timezone offset in + or - value in minutes.

This is problematic, because a time zone offset is not the same thing as a time zone. Consider that one cannot take the current offset and assume it is the correct offset to apply for any given date and time. Many time zones use more than one offset depending on time of year, and many time zones have had changes to their standard time offsets at different points in history. For more on this, see "Time Zone != Offset" in the timezone tag wiki .

As an example, you might be using a JavaScript function like new Date().getTimezoneOffset() . However, this returns the offset for the Date object returned by new Date() - which is the current date. For me, in the US Pacific time zone, it will return 480 (UTC-8) when run in the winter during standard time, but 420 (UTC-7) when run in the summer during daylight saving time.

Instead, have your front-end call Intl.DateTimeFormat().resolvedOptions().timeZone in JavaScript to return the time zone identifier, which for me returns "America/Los_Angeles" . You can pass that to your back-end and use it with PHP.

As a side note, there are no time zones in the world that use or have ever used UTC+0:15. The closest to that would have been UTC+0:20 used in the Netherlands from 1 May 1909 to 16 May 1940. This is already captured in the history of "Europe/Amsterdam" .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM