How do you handle the timezone difference in PHP?

I am told that this method below of calculating the user's local time sometimes does not work. What is the best way to do this in PHP? What are you doing?

public function getTimeOffset ($time) { $this->_cacheTimeOffset(); if ($this->timeOffsetExecuted) { $d = date("O"); $neg = 1; if (substr($d, 0, 1) == "-") { $neg = -1; $d = substr($d, 1); } $h = substr($d, 0, 2)*3600; $m = substr($d, 2)*60; return $time + ($neg * ($h + $m) * -1) + (($this->timeOffset + $this->getDstInUse()) * 3600); } return $time; } 
+4
source share
4 answers

Use a DateTime extension, for example DateTime :: getOffset ,
or DateTimeZone :: getOffset

In some countries, multiple time zone updates may be possible.
this DateTimeZone :: getTransitions method shows the transition history

+2
source
 date('Z'); 

returns the UTC offset in seconds.

+1
source

Just answered a very similar question here . I recommend you check this out; I explained the two preferred ways of calculating the time zone offset (using simple math and then the datetimezone and datetime classes) in some detail.

The first method would be the simplest (and most logical) way, namely to store their offset (if you already have it, that is) and multiply this by 3600 (1 hour in seconds), and then add this value to the current unix timestamp to get the final time running.

Another way to do this is to use datetime and datetimezone . How these two classes work, as shown here , is that you create two datetimezone objects, one with your time zone and one with them; Create two datetime objects with the first parameters "now" and the second - a link to datetimezone objects above (respectively); and then call getOffset in your time zone an object that passes its time zone object as the first parameter, eventually you get the offset in seconds, which can be added to the current unix to get the time for the work to be done.

0
source

Fast decision:

 <?php echo date('g:i a', strtotime("now + 10 hours 30 minutes")); ?> 
-1
source

All Articles