Advertisement
eventsmanager

Visitor Local Event Times

May 26th, 2019
1,049
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.70 KB | None | 0 0
  1. <?php
  2. /**
  3.  * 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:
  4.  *
  5.  * #_12HSTARTTIMELOCAL, #_12HENDTIMELOCAL, #_24HSTARTTIMELOCAL and #_24HENDTIMELOCAL
  6.  * This is purely determined by the user's browser settings and results may vary according to browser version.
  7.  *
  8.  * Another option is to generate links in formatting which point to a third party service, for example:
  9.  *  <a href="https://www.thetimezoneconverter.com/?t=#h%3A#i%20#a&tz=#_LOCATIONTOWN" target="_blank">See local time</a>
  10.  * Note that in this case for this website, the link depends on the fact that a valid city name is provided.
  11.  *
  12.  * For installation instructions, see here - http://wp-events-plugin.com/tutorials/how-to-safely-add-php-code-to-wordpress/
  13.  *
  14.  * @param string $replace
  15.  * @param EM_Event $EM_Event
  16.  * @param string $result
  17.  * @return string
  18.  */
  19. function my_em_event_local_time_placeholders($replace, $EM_Event, $result){
  20.     switch( $result ){
  21.         case '#_12HSTARTTIMELOCAL':
  22.         case '#_12HENDTIMELOCAL':
  23.             $rand = rand();
  24.             $ts = ($result == '#_12HSTARTTIMELOCAL') ? $EM_Event->start()->getTimestamp():$EM_Event->end()->getTimestamp();
  25.             ob_start();
  26.             ?>
  27.             <span id="em-start-local-time-<?php echo $rand ?>">Enable JS to see time...</span>
  28.             <script>
  29.                 var date = new Date(<?php echo $ts * 1000 ?>);
  30.                 var hours = date.getHours();
  31.                 var minutes = date.getMinutes();
  32.                 var ampm = hours >= 12 ? 'PM' : 'AM';
  33.                 hours = hours % 12;
  34.                 hours = hours ? hours : 12; // the hour '0' should be '12'
  35.                 minutes = minutes < 10 ? '0'+minutes : minutes;
  36.                 var strTime = hours + ':' + minutes + ' ' + ampm;
  37.                 document.getElementById("em-start-local-time-<?php echo $rand ?>").innerHTML = strTime;
  38.             </script>
  39.             <?php
  40.             $replace = ob_get_clean();
  41.             break;
  42.         case '#_24HSTARTTIMELOCAL':
  43.         case '#_24HENDTIMELOCAL':
  44.             $rand = rand();
  45.             $ts = ($result == '#_24HSTARTTIMELOCAL') ? $EM_Event->start()->getTimestamp():$EM_Event->end()->getTimestamp();
  46.             ob_start();
  47.             ?>
  48.             <span id="em-start-local-time-<?php echo $rand ?>">Enable JS to see time...</span>
  49.             <script>
  50.                 var date = new Date(<?php echo $ts * 1000 ?>);
  51.                 var hours = date.getHours();
  52.                 var minutes = date.getMinutes();
  53.                 hours = hours < 10 ? '0' + hours : hours;
  54.                 minutes = minutes < 10 ? '0' + minutes : minutes;
  55.                 document.getElementById("em-start-local-time-<?php echo $rand ?>").innerHTML = hours + ':' + minutes;
  56.             </script>
  57.             <?php
  58.             $replace = ob_get_clean();
  59.             break;
  60.     }
  61.     return $replace;
  62. }
  63. add_filter('em_event_output_placeholder','my_em_event_local_time_placeholders',1,3);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement