Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * Allows you to display local times on web pages (not emails etc.) relative to the user's browser time settings using 4 new placeholders similar to the placeholders showing 24/12 hour time formats:
- *
- * #_12HSTARTTIMELOCAL, #_12HENDTIMELOCAL, #_24HSTARTTIMELOCAL and #_24HENDTIMELOCAL
- * This is purely determined by the user's browser settings and results may vary according to browser version.
- *
- * Another option is to generate links in formatting which point to a third party service, for example:
- * <a href="https://www.thetimezoneconverter.com/?t=#h%3A#i%20#a&tz=#_LOCATIONTOWN" target="_blank">See local time</a>
- * Note that in this case for this website, the link depends on the fact that a valid city name is provided.
- *
- * For installation instructions, see here - http://wp-events-plugin.com/tutorials/how-to-safely-add-php-code-to-wordpress/
- *
- * @param string $replace
- * @param EM_Event $EM_Event
- * @param string $result
- * @return string
- */
- function my_em_event_local_time_placeholders($replace, $EM_Event, $result){
- switch( $result ){
- case '#_12HSTARTTIMELOCAL':
- case '#_12HENDTIMELOCAL':
- $rand = rand();
- $ts = ($result == '#_12HSTARTTIMELOCAL') ? $EM_Event->start()->getTimestamp():$EM_Event->end()->getTimestamp();
- ob_start();
- ?>
- <span id="em-start-local-time-<?php echo $rand ?>">Enable JS to see time...</span>
- <script>
- var date = new Date(<?php echo $ts * 1000 ?>);
- var hours = date.getHours();
- var minutes = date.getMinutes();
- var ampm = hours >= 12 ? 'PM' : 'AM';
- hours = hours % 12;
- hours = hours ? hours : 12; // the hour '0' should be '12'
- minutes = minutes < 10 ? '0'+minutes : minutes;
- var strTime = hours + ':' + minutes + ' ' + ampm;
- document.getElementById("em-start-local-time-<?php echo $rand ?>").innerHTML = strTime;
- </script>
- <?php
- $replace = ob_get_clean();
- break;
- case '#_24HSTARTTIMELOCAL':
- case '#_24HENDTIMELOCAL':
- $rand = rand();
- $ts = ($result == '#_24HSTARTTIMELOCAL') ? $EM_Event->start()->getTimestamp():$EM_Event->end()->getTimestamp();
- ob_start();
- ?>
- <span id="em-start-local-time-<?php echo $rand ?>">Enable JS to see time...</span>
- <script>
- var date = new Date(<?php echo $ts * 1000 ?>);
- var hours = date.getHours();
- var minutes = date.getMinutes();
- hours = hours < 10 ? '0' + hours : hours;
- minutes = minutes < 10 ? '0' + minutes : minutes;
- document.getElementById("em-start-local-time-<?php echo $rand ?>").innerHTML = hours + ':' + minutes;
- </script>
- <?php
- $replace = ob_get_clean();
- break;
- }
- return $replace;
- }
- add_filter('em_event_output_placeholder','my_em_event_local_time_placeholders',1,3);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement