0

I've got JavaScript code on my registration page that gets the timezone offset when they log in and sets it to a timezone offset variable in the database that looks like this "-06:00,1". The 1 being set if daylight savings time is observed.

My database is set to UK time or GMT +0:00, and I need to take the time from the database and use the user's timezone offset to get the correct timezone for where they are.

I've tried:

$timeSent = date('H:i:s', strtotime($servertime)+($tzoneOffset)

The above code works, but only if the timezone is a positive offset such as 06:00. How can I fix this?

Matt Johnson-Pint
  • 230,703
  • 74
  • 448
  • 575
Jsoft
  • 39
  • 5

3 Answers3

0

A simple if-then could solve this problem.

if UserTimeZone < 0
    userTime = serverTime - UserTimeZone

else 
    userTime = serverTime + UserTimeZone
Trendy
  • 460
  • 2
  • 12
0

Don't store them that way. There is much more to DST than just enabled/disabled.

PHP has full support for IANA time zones. Use those instead.

For example, use Europe/London or America/Los_Angeles.

You can't just say DST enabled/disabled because so many time zones have different rules about when DST goes into effect, and by how much they are offset. There is no world-wide standard.

There are a couple of other points you need to be aware of:

  • The UK is not always on GMT +0:00. In the summer, British Summer Time (BST) goes into effect, which is +1:00. It would be more appropriate for your database server to be set to UTC - which is essentially the same as GMT, and never changes offsets.

  • You say you are gathering the time zone offset from your user via JavaScript. While this is possible, it's not good practice. When you do so, you are only capturing the offset that currently applies. You should instead consider having your user select their time zone, either from a drop-down list, or using a map-based time zone picker, such as this one. You might consider using jsTimeZoneDetect to guess a default value for the list, but don't rely on it exclusively. Your user should have the ability to change it.

  • Never ever ask the user if they want DST or not. Most end-users are clueless about the rules of their time zones. Let PHP handle your DST concerns.

If you haven't already, please review the time zone tag wiki. There is a lot of good information there.

Community
  • 1
  • 1
Matt Johnson-Pint
  • 230,703
  • 74
  • 448
  • 575
  • If you could explain how I could convert the timezone on the database using the php timezones I'd gladly do that but I can't figure it out – Jsoft Jul 13 '13 at 04:32
  • Do the conversion in your PHP code, not in the database. There are examples in the PHP manual [here](http://www.php.net/manual/en/book.datetime.php). – Matt Johnson-Pint Jul 13 '13 at 06:07
  • If you need a more direct example, there are plenty already on StackOverflow. For example, [this one](http://stackoverflow.com/q/5746531/634824). – Matt Johnson-Pint Jul 13 '13 at 06:16
0

Assuming that your database time is a UNIX timestamp value (or it could be), you could use a function like this to take the offset apart and add the seconds (+ or -) to the dbtime:

function adjustTime($dbtime, $offset) {
    $hm = explode(":", $offset);
    $dl = explode(",", $hm[1]);

    $newtime = $dbtime + (intval($hm[0])*3600) + ($dl[1]*3600);

    return $newtime;
}

Also assumed that your daylight savings value will be zero if not applied.

bitfiddler
  • 2,095
  • 1
  • 12
  • 11