iCalReader add-on for Concerts and events


/ Published in: PHP
Save to your folder(s)

You can find the ical reader here: http://www.phps.com/scripts/iCalReader.php


Copy this code and paste it in your HTML
  1. <table id="cal">
  2.  
  3. <?php
  4. # you need the iCalReader found here:
  5. # http://www.phps.com/scripts/iCalReader.php
  6.  
  7. // These define() lines go at the top of the script
  8. define('DTSTART', 'DTSTART;TZID=Europe/Oslo');
  9. # set the number of events you want to show
  10. define('MAX_EVENTS', 100);
  11.  
  12. // Use the DTSTART constant in the compare_date() function
  13.  
  14. require_once 'iCalReader.php';
  15.  
  16. $ical = new iCalReader('konserter.ics');
  17. $meta = $ical->getMeta();
  18. $events = $ical->getEvents();
  19.  
  20. #compare dates function
  21. function compare_date($a, $b)
  22. {
  23. return strnatcmp($a['DTSTART;TZID=Europe/Oslo'], $b['DTSTART;TZID=Europe/Oslo']);
  24. }
  25.  
  26. # sort arrays after the dates
  27. usort($events, 'compare_date');
  28.  
  29.  
  30.  
  31. // Get and sort the events here
  32.  
  33. // This needs to be calculated only once, so put it outside the loop
  34. $today = date('Ymd');
  35.  
  36. // Save the count to a variable to avoid unnecessary calculations
  37. $count = count($events);
  38.  
  39. // Loop through all events until either there are no more events or
  40. // the maximum number of recent events has been found
  41. for ($row = 0, $hits = 0; $row < $count && $hits < MAX_EVENTS; $row++) {
  42.  
  43. // It's possible that some events will not have the appropriate key,
  44. // so skip those events
  45. if (! array_key_exists(DTSTART, $events[$row])) {
  46. continue;
  47. }
  48.  
  49. $date = substr($events[$row][DTSTART], 0, 8);
  50.  
  51. if ($date >= $today) {
  52.  
  53. // Increment the number of events found
  54. $hits++;
  55.  
  56. // Move these operations inside this block to avoid unnecessary calculations
  57. $year = substr($events[$row][DTSTART], 0, 4);
  58. $month = substr($events[$row][DTSTART], 4, 2);
  59. $day = substr($events[$row][DTSTART], 6, 2);
  60. //$time = substr($events[$row][DTSTART], 9, 4); // Unused?
  61.  
  62. // This looks cleaner (in my opinion), but the format does not matter
  63. echo '<tr>'
  64. . '<td class="summary">' . $events[$row]['SUMMARY'] . '</td>'
  65. . '<td class="date">'
  66. . '<span class="day">' . $day . '/</span>'
  67. . '<span class="month">' . $month . '/</span>'
  68. . '<span class="year">' . $year . '</span>'
  69. . '</td>'
  70. . '</tr>' . "\n";
  71. }
  72. } ?>
  73. </table>

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.