ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

Showing different time zones with a date variable

September 01, 2011 7:19pm

Subscribe [5]
  • #1 / Sep 01, 2011 7:19pm

    Dave Joyce

    49 posts

    I’ve looked over the documentation ( http://ellislab.com/expressionengine/user-guide/templates/date_variable_formatting.html ), and I’m not sure where else to look for this, but is there a way to show multiple time zones with one date variable field?

    My server time is set to GMT and it’d be really nice to set one time, but then display the times for EST, CET, the UK…. actually, what would be SUPER cool is if there were a way to detect the client’s time zone and show a local time, but I know that would have to be done on the front end.

    Thanks for any help or guidance you can offer.

  • #2 / Sep 02, 2011 9:17am

    Sue Crocker

    26054 posts

    Hi Dave Joyce,

    Have you tried %T at all? Where are you wanting this to show?

  • #3 / Sep 02, 2011 10:14am

    Dave Joyce

    49 posts

    Yes, I’ve used %T for displaying the time zone, but I’d like to offset the entire time variable a certain amount so I can display a different time zone.

    It’s a variable for a webinar. And the site is loaded throughout the world. So if the webinar takes place at 5PM in the UK it would take place at say 10AM in New York for example. And I need to display both times. It would look like this:

    August 13th, 2011
    5pm CET, 4pm UK, 2pm EDT

  • #4 / Sep 02, 2011 11:32am

    stevenkovar

    11 posts

    I used PHP (date_default_timezone_set) to do this on a project. Basically, I needed to do the opposite from a time string (strip timezone formatting), so I set to ‘UTC’, but there is a list of timezone identifiers in the PHP manual. I just needed time, so you’ll have to change the date format variables to format the whole date, then echo those PHP variables where you need them. Maybe it’s not the best way, but it worked for me. I was under the impression you can’t do time zone offsetting with EE.

    <?php 
    date_default_timezone_set('UTC'); 
    $UTCstart = {my_custom_field}; 
    echo date("G",$UTCstart);
    echo ':'; 
    echo date("i",$UTCstart);
    ?>
  • #5 / Sep 07, 2011 2:38am

    John Henry Donovan

    12339 posts

    Dave,

    There are no EE tags to cover timezone offsetting in the way that you wish. It is recommended that you use some PHP or create a plugin as Steven suggest here for your needs. Does that help?

  • #6 / Sep 07, 2011 5:20pm

    Dave Joyce

    49 posts

    Thanks Stevenkovar. I think that script will do the trick. I’d prefer to not enable PHP, but it’ll work in this case.

    However, I’m not sure I’m getting the right results for some reason.

    I have

    <?php
    date_default_timezone_set('Europe/London'); 
    $timezonedate = {webinar-date}; 
    echo date("g",$timezonedate);
    echo ':'; 
    echo date("i a",$timezonedate);
    echo " GMT, ";
    ?>
    
    {webinar-date format="%G %T"}

    The PHP code outputs 6:00pm GMT and the EE code outputs 5:00pm GMT.

    The server time is set to UTC 0 (GMT) and the time in the variable is set to 5pm. So why would the date_default_timezone_set for Europe London put it ahead one hour to 6pm?

  • #7 / Sep 07, 2011 7:13pm

    stevenkovar

    11 posts

    Hi Dave, I’m not sure of the settings of your install/server, but I’m guessing that the hour difference might be daylight savings time?

    If your server (thus the time in your field) is set to GMT (no DST offset), and you set the timezone for output to “Europe/London”, then I assume it’s adding an hour for DST (spring ahead one hour). London is UTC +1 in summer, and UTC in winter, I think.

    I would set the default timezone to UTC, then set some variables that offset this for different timezones. It’s a tricky question - you can deal with this in PHP, but I really don’t know how (maybe with timezone_offset_get).  You’ll have to make sure that what you devise still works after the next DST change - hard-coding offsets that aren’t responsive to DST changes could break down in the future, and would in this case.

  • #8 / Sep 08, 2011 8:25pm

    Dan Decker

    7338 posts

    Dave,

    I think stevenkovar is on the right track with his suggestions. Can you let us know your outcome?

    Cheers,

  • #9 / Sep 09, 2011 2:53pm

    Dave Joyce

    49 posts

    Well I set the date back to UTC instead of Europe/London so I get 5PM (as entered)  instead of 6PM, and I’ve got it sort of working. Not sure of all the variables, but since I assume London and Paris follow the same DST schedule, and because the time in EE is entered in GMT time, those should be safe to do a simple hard offset.

    New York however seems to be variable. It’s normally minus 5 hours, but right now it seems to be minus 4. So I’m using the timezone_set function which seems to be able to account for that difference. I *think* this will continue to work throughout the year. Unless there’s a DST variable I’m not considering.

    <?php 
        $timezonedate = {entry_date};
        $onehour = 3600;
        
        date_default_timezone_set('UTC'); 
        
        /* GMT */
            echo date("g",$timezonedate);
            echo ':'; 
            echo date("ia",$timezonedate);
            echo " GMT, ";
            
        /* CET: UTC+1 */
            $UTC_time = $timezonedate + $onehour;
            echo date("g",$UTC_time);
            echo ':'; 
            echo date("ia",$UTC_time);
            echo " CET, ";
    
        /* EST */
            date_default_timezone_set('America/New_York'); 
            echo date("g",$timezonedate);
            echo ':'; 
            echo date("ia",$timezonedate);
            echo " EST";
            
        ?>
  • #10 / Sep 12, 2011 2:57am

    John Henry Donovan

    12339 posts

    Thanks for sharing this Dave. I’m going to move this one to Community Help as it may help others and it will leave the thread open for you if you wish to come back to it at any stage.

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases