eventsmanager

Event Duration

Feb 22nd, 2013
183
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  
  3. Custom placeholder to get date/time difference between event start date and event end date
  4.  
  5. */
  6. add_filter('em_event_output_placeholder','my_em_days_duration_placeholders',1,3);
  7. function my_em_days_duration_placeholders($replace, $EM_Event, $result){
  8. global $wp_query, $wp_rewrite;
  9. switch( $result ){
  10. case '#_EVENTDURATION':
  11. if ( !$EM_Event->event_all_day ){
  12. $replace = dateDiff($EM_Event->start,$EM_Event->end);
  13. }else{
  14. $replace = "Whole Day Event!";
  15. }
  16. break;
  17. }
  18. return $replace;
  19. }
  20.  
  21. function dateDiff($time1, $time2, $precision = 6) {
  22. // If not numeric then convert texts to unix timestamps
  23. if (!is_int($time1)) {
  24. $time1 = strtotime($time1);
  25. }
  26. if (!is_int($time2)) {
  27. $time2 = strtotime($time2);
  28. }
  29.  
  30. // If time1 is bigger than time2
  31. // Then swap time1 and time2
  32. if ($time1 > $time2) {
  33. $ttime = $time1;
  34. $time1 = $time2;
  35. $time2 = $ttime;
  36. }
  37.  
  38. // Set up intervals and diffs arrays
  39. $intervals = array('year','month','day','hour','minute','second');
  40. $diffs = array();
  41.  
  42. // Loop thru all intervals
  43. foreach ($intervals as $interval) {
  44. // Set default diff to 0
  45. $diffs[$interval] = 0;
  46. // Create temp time from time1 and interval
  47. $ttime = strtotime("+1 " . $interval, $time1);
  48. // Loop until temp time is smaller than time2
  49. while ($time2 >= $ttime) {
  50. $time1 = $ttime;
  51. $diffs[$interval]++;
  52. // Create new temp time from time1 and interval
  53. $ttime = strtotime("+1 " . $interval, $time1);
  54. }
  55. }
  56.  
  57. $count = 0;
  58. $times = array();
  59. // Loop thru all diffs
  60. foreach ($diffs as $interval => $value) {
  61. // Break if we have needed precission
  62. if ($count >= $precision) {
  63. break;
  64. }
  65. // Add value and interval
  66. // if value is bigger than 0
  67. if ($value > 0) {
  68. // Add s if value is not 1
  69. if ($value != 1) {
  70. $interval .= "s";
  71. }
  72. // Add value and interval to times array
  73. $times[] = $value . " " . $interval;
  74. $count++;
  75. }
  76. }
  77.  
  78. // Return string with times
  79. return implode(", ", $times);
  80. }
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×