• Resolved Nate Angell

    (@ixmati)


    In snippets under Settings > Formatting, we are trying to figure out if there is a way to format the timezone for placeholders #EVENTTIMES and #_EVENTTIMES_LOCAL so it only appears once at the end of the start–end time range.

    So instead of something like “16:00 (UTC-07:00)–17:00 (UTC-07:00)”, it would be like “16:00–17:00 (UTC-07:00)”.

    We could use #_EVENTSTARTTIME–#_EVENTENDTIME to show just the times without timezones, but there doesn’t seem to be a way to show the timezone of an event independently of #_EVENTTIMES.

    The page I need help with: [log in to see the link]

Viewing 8 replies - 1 through 8 (of 8 total)
  • In Settings > Formatting set the time format without the timezone. Then you could create a custom placeholder called #_MYEVENTTIMES using the following code snippet:

    add_filter('em_event_output_placeholder', function($replace, $EM_Event, $result) {
        if (preg_match('/#_MYEVENTTIMES.*/', $result)) {
             $replace = $EM_Event->output_times() . '(' . $EM_Event->start()->i18n('e') . ')';
        }
        return $replace;
    }, 10, 3);

    You could use the Code Snippets plugin to add this code snippet.

    Here’s a tutorial on creating custom placeholders: https://wp-events-plugin.com/documentation/tutorials/create-a-custom-placeholder-for-event-formatting/

    Thread Starter Nate Angell

    (@ixmati)

    Thank you so much! That works great as an alternative for #_EVENTTIMES, although it also includes the timezone on “all day” events, which is unnecessary — I’ve been looking to see if there might be a way around that.

    We are also trying to figure out how to do the same thing as an alternative for #_EVENTIMES_LOCAL, but am not quite sure the variable to modify for that placeholder.

    I’m wondering if it might not be easier to just create a custom placeholder just for the event timezone and the event local timezone so they could be output independently of the start/end times…

    Thread Starter Nate Angell

    (@ixmati)

    I came up with this as a function to supply a placeholder with just the local timezone. Happy to hear suggestions for improvement!

    add_filter('em_event_output_placeholder', function($replace, $EM_Event, $result) {
        if (preg_match('/#_MYEVENTTIMEZONE_LOCAL.*/', $result)) {
    	$rand = rand();
    	ob_start();
    	?>
    	<span id="em-start-local-timezone-<?php echo $rand ?>">JavaScript Disabled</span>
    	<script>
    		document.getElementById("em-start-local-timezone-<?php echo $rand ?>").innerHTML = Intl.DateTimeFormat().resolvedOptions().timeZone;
    	</script>
    	<?php
    	$replace = ob_get_clean();
        }
        return $replace;
    }, 10, 3);
    Thread Starter Nate Angell

    (@ixmati)

    Also, if I’m not mistaken there is already an undocumented #_EVENTTIMEZONE placeholder, so it seems like one could solve the first need here using something like:

    #_24HSTARTTIME–#_24HENDTIME (#_EVENTTIMEZONE)
    Thread Starter Nate Angell

    (@ixmati)

    I guess there is also an undocumented #_EVENTTIMEZONE_LOCAL placeholder, so it seems like one could solve the same need for local times using something like:

    #_24HSTARTTIME_LOCAL–#_24HENDTIME_LOCAL #_EVENTTIMEZONE_LOCAL

    The only issue is that because of the JS scripting, #_EVENTTIMEZONE_LOCAL results have whitespace around them and so putting characters around the placeholder like:

    (#_EVENTTIMEZONE_LOCAL)

    displays something like:

    ( #_EVENTTIMEZONE_LOCAL )

    Glad to hear you were able to figure this out. I don’t know how to get rid of the spaces.

    Thread Starter Nate Angell

    (@ixmati)

    Thanks for your help! It solved the initial need and set me off on the right track for another solution!

    Thread Starter Nate Angell

    (@ixmati)

    For anyone looking at this in the future, due to JS issues with _24HSTARTTIME_LOCAL and #_24HENDTIME_LOCAL (see this issue and a workaround) I ended up using the following placeholders to show the timezone just once at the end of event times. Note that this solution requires that the timezone NOT be included in the format at Settings >Formatting > Date/Time > Time Format (I use “H:i” here to format times).

    #EVENTTIMES #_EVENTTIMEZONE
    #EVENTTIMES_LOCAL #_EVENTTIMEZONE_LOCAL
Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘formatting placeholder timezones only once at end’ is closed to new replies.