Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Apr 2012
    Posts
    89
    Thanks
    15
    Thanked 0 Times in 0 Posts

    Timezones and displaying

    Hi all,

    Simple question, probably, but I cannot seem to find an answer anywhere. In my database I am saving the "start time" of events like below:

    Code:
    12:00
    Now I have the following issue. I want users to display the time in their own timezone. I accomplished this using the "Europe/Amsterdam" functoin, but I am now integrating my script into my Invision Power Board Forums, which is using a different approach to timezones. The IPB Forums save the timezone as "1" or "-1" for example.

    Example. In my database the start time has been stored as 1:00. Doesn't matter if it's PM or AM. That is the time in GMT. A user's timezone is "+5". How can I display the start time of the event in their own timezone? They should be seeing "04:00". I cannot take the "11:00" and just add +5 to it, because it would end up like "16:00" instead of "04:00".

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,978
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    IPB is running oldschool. Before DateTime we had few options other than providing it with a +/- offset.
    Dealing with it is relatively easy. I don't know how you'll bridge it or adapt it properly with the software to get there, but doing as such:
    PHP Code:
    $dt = new DateTime('12:00');
    $dt->setTimezone(new DateTime('America/Regina')); // my time zone
    print $dt->format('H:i:s'); // 06:00:00 
    Is the same as me going:
    PHP Code:
    $dt = new DateTime('12:00');
    $dt->modify('-6 hour');
    print 
    $dt->format('H:i:s'); 
    Assuming UTC (which it should be by default).
    BUT! There's one thing that is not accommodated here, and that is DST corrections. You'll need to provide that as another modify (where you calculate the DST based on the time of year and whether to go forward or back). That's only useful if there is a way to actually determine if DST correction is available. Most places have DST; where I live we do not.

    Also, one thing to note is your choice of time. 01:00 is only equal to 1:00AM, not 1:00PM. There is no way to determine if 01:00 is am or pm, unless you're ruleset dictates a 24 hour clock. Although you're example appears it should be 11:00 and not 1:00 based on the rest, if you provide 11:00 that is equal to 16:00 with a +5, not 04:00. To get to 04:00, you must specify 11:00AM or 23:00.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #3
    New Coder
    Join Date
    Apr 2012
    Posts
    89
    Thanks
    15
    Thanked 0 Times in 0 Posts
    Thanks for the help, although I figured it out by myself in the end, just finished playing with it. Here's what I did (just copied my test snippet / page):

    PHP Code:
    <?php
    $starttime    
    "22:00";
    $time         strtotime('22:00');

    $start1     date("H:i"strtotime('-3 hours'$time));
    $start2     date("H:i"strtotime('+3 hours'$time));

    $time_format1     "g:i A";
    $time_format2     "G:i";
    ?>

    <br /><br />

    GMT Start: <?php echo "$starttime"?><br /><br />

    12-hour: <?php echo date("$time_format1"strtotime($start1)); ?>
    <br />
    12-hour: <?php echo date("$time_format1"strtotime($start2)); ?>

    <br /><br />

    24-hour: <?php echo date("$time_format2"strtotime($start1)); ?>
    <br />
    24-hour: <?php echo date("$time_format2"strtotime($start2)); ?>


  •  

    Posting Permissions

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