Change current user time zone based on server UTC offset and user UTC offset

im write twitter web service in php. When the user subscribes, I get this node:

<utc_offset>-18000</utc_offset> 

I need to change the time zone of the script so that it adapts to the user time zone. The only php function I found for this is: date_default_timezone_set ($ timezone_identifier), but that will not allow me to use -18000 as the $ timezone_identifier parameter.

So, how can I change the user's current time zone based on two values: Server UTC Offset and User UTC Offset

By the way, here is how I get the UTC server offset value:

 $this_tz_str = date_default_timezone_get(); $this_tz = new DateTimeZone($this_tz_str); $now = new DateTime("now", $this_tz); $offset = $this_tz->getOffset($now); 

Any ideas? Thanks!

+4
source share
2 answers

To get the current server time

 date_default_timezone_set(date_default_timezone_get()); echo date('Ymd H:i:s', time()); 

Exit for Europe / Paris (my server settings, UTC + 2)

 2011-04-12 20:39:43 

To get user time using offset

 $user_offset = '-18000'; date_default_timezone_set('UTC'); $diff = "$user_offset seconds"; if ((substr($diff,0,1) != '+') && (substr($diff,0,1) != '-')) $diff = '+' . $diff; $usertime = strtotime($diff, time()); echo date('Ymd H:i:s', $usertime); 

The UTC-5 exit (Ecuador → Quito NO DST Time), time zone identifier for php is 'America/Guayaquil' .

 2011-04-12 13:39:43 

PHP.net Guide:

Timezone offset in seconds. The offset for the time zones west of UTC is always negative, and for the east of UTC it is always positive. (-43200 50400)

+2
source

The date_default_timezone... functions expect the string to give something like "Africa / Luanda" or something else.

I suggest programmatically searching the time zone database for the corresponding offset. If I remember correctly, they are minutes from UTC, so you should divide the offset you give by 60.

+1
source

Source: https://habr.com/ru/post/1347779/


All Articles