SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Guru
    Join Date
    Apr 2007
    Posts
    685
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Converting times depending on user timezone

    I am needing to convert date/timezone to an end user. I am trying to write a script that will allow professionals to setup a meeting time in a database in their timezone but automatically convert to other users in their stored timezone:

    So I have a meeting time database:


    Date -Time - Timezone

    11-15-2012 - 9:00 P.M - America/Indiana/Tell_City
    11-15-2012 - 10:00 A.M - America/Kentucky/Louisville
    11-15-2012 - 7:00 A.M - America/Chicago

    The end user has their timezone stored in the database

    Username - TimeZone

    Test - America/Indianapolis



    How would I get the data from the first table of meeting for instance that is in the America/Chicago timezone to automatically show in the America/Indianapolis timezone of the end user?

  2. #2
    Always A Novice bronze trophy
    K. Wolfe's Avatar
    Join Date
    Nov 2003
    Location
    Columbus, OH
    Posts
    2,182
    Mentioned
    65 Post(s)
    Tagged
    2 Thread(s)
    I'd make sure that all program logic uses the same (server) time zone. Create a timezone table with timezone name and timezone offset (positive or negative integer) compared to your server timezone. If the user has specified a desired timezone in the user table, then apply the correction from the integer in your table.

    users
    ----
    user_id
    user_name
    time_zone_name

    time_zones
    ----
    time_zone_name
    time_zone_offset int


    Code PHP:
    //login form, define users time zone offset compared to your db
    // $result = SQL result 'select time_zone_offset from time_zones t inner  join users u on t.time_zone_name = u.time_zone_name where u.user_id =  $userId' 0 if null or not defined
    $_SESSION['userTimeZoneOffset']  = $result;

    Code PHP:
    //any location where times are displayed
    if ($_SESSION['userTimeZoneOffset'] != 0) {
    $meetingTime = date($meetingTime, strtotime ($_SESSION['userTimeZoneOffset'] . " hour"));
    }

    Untested of course, but this is a general idea of how it can be done. I'm a little iffy on the strtotime() function syntax as I don't think I've had to use it before.

  3. #3
    Always A Novice bronze trophy
    K. Wolfe's Avatar
    Join Date
    Nov 2003
    Location
    Columbus, OH
    Posts
    2,182
    Mentioned
    65 Post(s)
    Tagged
    2 Thread(s)

  4. #4
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.



Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •