Page 1 of 1

iCalReader problem

Posted: Thu Aug 26, 2010 11:53 am
by stav
Hi,
this is my first topic in this forum :)
I'm having some trouble with iCalReader. I want to display the upcoming dates, and in this case only the first 6 dates. But the problem is that with the code shown below, it counts 6 dates of all the dates, not only the upcoming ones. I hope you understand.

Any easy solution to this?

Code: Select all

<?php
# you need the iCalReader found here:
# http://aidanlister.com/repos/v/iCalReader.php

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');

for ($row = 0; $row < 6; $row++)
{
$today	= date('Ymd');
$date	= substr($events[$row]["DTSTART;TZID=Europe/Oslo"], 0, ;
$year = substr($events[$row]["DTSTART;TZID=Europe/Oslo"], 0, 4);
$month = substr($events[$row]["DTSTART;TZID=Europe/Oslo"], 4, 2);
$day = substr($events[$row]["DTSTART;TZID=Europe/Oslo"], 6, 2);
$time	= substr($events[$row]["DTSTART;TZID=Europe/Oslo"], 9,4);



if ($date >= $today){

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>";
}
}
?>

Re: iCalReader problem

Posted: Thu Aug 26, 2010 2:15 pm
by stav
So I need a way do not read the past dates. Or only read the upcoming dates.

Could i just set an if statement somewhere?

Re: iCalReader problem

Posted: Thu Aug 26, 2010 2:42 pm
by McInfo
I should really make you write this yourself, but I needed some practice.

Code: Select all

// These define() lines go at the top of the script
define('DTSTART', 'DTSTART;TZID=Europe/Oslo');
define('MAX_EVENTS', 6);

// Use the DTSTART constant in the compare_date() function

// 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";
    }
}

Re: iCalReader problem

Posted: Thu Aug 26, 2010 6:56 pm
by stav
Yeah, you probably should have :) but than's a lot! Is it ok if I post it all to snipplr?

Re: iCalReader problem

Posted: Thu Aug 26, 2010 7:44 pm
by McInfo
stav wrote:Is it ok if I post it all to snipplr?
I don't think it's generic enough to be useful to anyone but you, but I can't/won't stop you from spreading it.

Re: iCalReader problem

Posted: Thu Aug 26, 2010 8:57 pm
by stav
It's perfect for bands and such! You can set iCal to publish a calendar automatic, so then the artist/label don't have to worry about updating their webpage. I've used it here: http://www.brilliance.no And they love it.