5

I'm being told that this below method of calculating the user's local time is sometimes not working. What's the best way to do this in PHP? What do you do?

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;
}
Ben
  • 60,438
  • 111
  • 314
  • 488

4 Answers4

2

Use the DateTime extension, such as DateTime::getOffset,
or DateTimeZone::getOffset

Some countries might have perform several timezone update,
this method DateTimeZone::getTransitions reveal the transition history

Qix - MONICA WAS MISTREATED
  • 14,451
  • 16
  • 82
  • 145
ajreal
  • 46,720
  • 11
  • 89
  • 119
  • I'm currently allowing a user to specify their timezone in their profile. Are you saying that I should just be able to pull it dynamically via PHP and don't have to ask them their timezone? – Ben Nov 29 '10 at 14:34
  • 1
    @Webnet - You still need to user to place their current timezone into their profile. Probably, you can provide a list of timezone, and let let user to choose. `DateTimeZone::listIdentifiers` able to provide a full list of timezone supported, details - http://www.php.net/manual/en/datetimezone.listidentifiers.php – ajreal Nov 29 '10 at 18:28
1
date('Z');

returns the UTC offset in seconds.

Qix - MONICA WAS MISTREATED
  • 14,451
  • 16
  • 82
  • 145
kachar
  • 2,310
  • 30
  • 32
1

Just answered a very similar question over here. I recommend you check that one out; I explained the two preferred ways of doing timezone offset calculation (using simple math, and then the datetimezone and datetime classes) pretty thoroughly.

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

Another way to do it is to use the DateTime and DateTimeZone classes. How these two classes work, as shown here, is that you create two DateTimeZone objects, one with your timezone and one with theirs; create two DateTime objects with the first parameters being "now" and the second being the reference to the DateTimeZone objects above (respectively); and then call the getOffset method on your timezone object passing their timezone object as the first parameter, ultimately getting you the offset in seconds that can be added to the current unix timestamp to get the time that their job needs to run.

Community
  • 1
  • 1
Qix - MONICA WAS MISTREATED
  • 14,451
  • 16
  • 82
  • 145
-1

A quick solution:

<?php echo date('g:i a', strtotime("now + 10 hours 30 minutes")); ?>
RabidFire
  • 6,280
  • 1
  • 28
  • 24