0

I have a custom field where the publisher can choose what time zone they want the entry date to display as, but I'm having trouble when trying to use it in the template:

{exp:channel:entries
    channel='events'
}
    {entry_date format='%g:%i%a %T' timezone='{events_tz}'}
{/exp:channel:entries}

Since both tags are parsed at the same time, the {events_tz} field doesn't get parsed in time to be applied as a parameter to the date field.

How would I accomplish this? I'm willing to use Stash, if necessary.

1

If I'm inferring correctly, there are events in multiple time zones and they want each to show in the time that is local to the location of the event. If so, I think what you want is Fixed instead of Localized, and just output the time zone next to the date.

If you must use entry date, which will always be localized, you could create a simple plugin that accepts the UTC timestamp and the timezone and return a formatted date, e.g.:

{exp:your_date_plugin timestamp='{entry_date}' timezone='{events_tz}'}
6
  • I'm not using a date field type, though. Just using the entry's entry_date.
    – kmgdev
    Mar 25 '15 at 23:16
  • Ok, I've updated my answer for another option with entry dates. If you don't know how to make the plugin, comment back here and I'll revise my answer later with a complete solution. Gotta run out the door right now though! Mar 26 '15 at 4:35
  • I think I can handle that!
    – kmgdev
    Mar 26 '15 at 16:40
  • I'm guessing I can use EE's existing date functions - is parse_date_variables() the one I should be working with?
    – kmgdev
    Mar 30 '15 at 15:47
  • You will want format_date since you are supplying your plugin with a UTC Unix timestamp. https://ellislab.com/expressionengine/user-guide/development/usage/localization.html#Localize::format_date Mar 31 '15 at 16:47
0

Following Derek's advice above, I wrote a quick plugin to handle the conversion. Here's the plugin method code (note: I used the EE Add-On Builder for Sublime Text to generate the plugin file and boilerplate code):

public function convert_timezone() {
    $timestamp = ee()->TMPL->fetch_param('timestamp');
    $timezone = ee()->TMPL->fetch_param('timezone');
    $format = ee()->TMPL->fetch_param('format');

    return ee()->localize->format_date($format, $timestamp, $timezone);
}

And the tag it uses:

{exp:site_utilities:convert_timezone 
    timestamp='{entry_date}' 
    timezone='{events_tz}' 
    format='%g:%i%a %T'
 }
1
  • Nicely done! Thanks for sharing your complete solution. Mar 31 '15 at 22:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.