Return to Snippet

Revision: 31023
at August 27, 2010 12:06 by stav


Initial Code
<table id="cal">
				
	<?php
	# you need the iCalReader found here:
	# http://www.phps.com/scripts/iCalReader.php
	 
	 // These define() lines go at the top of the script
	define('DTSTART', 'DTSTART;TZID=Europe/Oslo');
	# set the number of events you want to show
	define('MAX_EVENTS', 100); 
	
	// Use the DTSTART constant in the compare_date() function
		 
	require_once 'iCalReader.php';
	 
	$ical = new iCalReader('konserter.ics');
	$meta = $ical->getMeta();
	$events = $ical->getEvents();
	
	#compare dates function
	function compare_date($a, $b)
	{
	  return strnatcmp($a['DTSTART;TZID=Europe/Oslo'], $b['DTSTART;TZID=Europe/Oslo']);
	}
	
	# sort arrays after the dates
	usort($events, 'compare_date');
	
	

	// Get and sort the events here
	
	// This needs to be calculated only once, so put it outside the loop
	$today = date('Ymd');
	
	// Save the count to a variable to avoid unnecessary calculations
	$count = count($events);
	
	// Loop through all events until either there are no more events or
	// the maximum number of recent events has been found
	for ($row = 0, $hits = 0; $row < $count && $hits < MAX_EVENTS; $row++) {
	
	    // It's possible that some events will not have the appropriate key,
	    // so skip those events
	    if (! array_key_exists(DTSTART, $events[$row])) {
	        continue;
	    }
	    
	    $date = substr($events[$row][DTSTART], 0, 8);
	
	    if ($date >= $today) {
	
	        // Increment the number of events found
	        $hits++;
	
	        // Move these operations inside this block to avoid unnecessary calculations
	        $year  = substr($events[$row][DTSTART], 0, 4);
	        $month = substr($events[$row][DTSTART], 4, 2);
	        $day   = substr($events[$row][DTSTART], 6, 2);
	      //$time  = substr($events[$row][DTSTART], 9, 4); // Unused?
	        
	        // This looks cleaner (in my opinion), but the format does not matter
	        echo '<tr>'
	           . '<td class="summary">' . $events[$row]['SUMMARY'] . '</td>'
	           . '<td class="date">'
	           . '<span class="day">'   . $day   . '/</span>'
	           . '<span class="month">' . $month . '/</span>'
	           . '<span class="year">'  . $year  .  '</span>'
	           . '</td>'
	           . '</tr>' . "\n";
	    }
	}	?>
</table>

Initial URL


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

Initial Title
iCalReader add-on for Concerts and events

Initial Tags


Initial Language
PHP