PhpDig.net

What is PhpDig?
PhpDig is a PHP MySQL based
Web Spider & Search Engine.




mcal_next_recurrence

Name

mcal_next_recurrence — Returns an object filled with the next date the event occurs, on or after the supplied date. Returns empty date field if event does not occur or something is invalid.

Synopsis

object mcal_next_recurrence(MCALconnection, weekstart, next);
int MCALconnection: An open MCAL connection
int weekstart: The first day of the week, usually MCAL_SUNDAY or MCAL_MONDAY
array next: An array containing the date to search for recurrences after

Returns

A date object containing the next recurrence of the event, or an empty object if there are no more recurrences

Description

You need to use mcal_next_recurrence() along with mcal_list_events() to get multiple recurrences of a single event inside a date ranage. The next parameter is an array containing three keys: year, month, and mday. These denote the date after which recurrences should be returned.

Note

mcal_next_recurrance() does not take an event ID parameter. This is a little bit non-intuitive. Instead, it returns the next recurrance of whatever event is in the global event structure - usually, the event you have most recently fetched with mcal_fetch_event() . Because of this, you must call mcal_next_recurrence() to get all of the recurrences of an event that you need to know about before calling mcal_fetch_event() on the next event.

Warning

The weekstart parameter should not affect the results of this function. However, libmcal has been mainly tested with the assumption that the week starts on Sunday, and there is an obscure bug that can cause recurrences to be missed during the last week of December, 2000, and possibly other dates. So just use MCAL_SUNDAY as the start of the week, and go ahead and code your applications to start weeks whenever you want.

Version

PHP 3 since 3.0.13, PHP 4 since 4.0b4

Example

Example 727. Finding all events in a month, including events that occur more than once

$calendar = "{/mstore}";
$MCALconnection = mcal_open($calendar, "username", "password");

// List all events during May, 2001.
$year = 2001;
$month = MCAL_MAY;
$daysInMonth = mcal_days_in_month($month,
                                  mcal_is_leap_year($year));

// Get the unique list of all events in the month. This list
// will only have one entry for each recurring event, so we'll
// need additional logic to get all recurrences of each event.
$eventIDs = mcal_list_events($MCALconnection,
                           2001, $month, 1,
                           2001, $month, $daysInMonth);

$startOfMonthTimestamp = mktime(0, 0, 0, $month, 1, $year);

$monthData = array();
foreach ($eventIDs as $eventID) {
    $event = mcal_fetch_event($MCALconnection, $eventID);
    if ($event->recur_type != MCAL_RECUR_NONE) {
        // We need to make sure that we only ask for recurrences
        // inside the month.
        if (mktime(0,0,0,
                   $event->start->month,
                   $event->start->mday,
                   $event->start->year) <= $startOfMonthTimestamp) {
            $mday = 1;
        } else {
            $monthData[$event->start->mday][$event->id] = $event->title;
            $mday = date('j',
                         mktime(0,0,0,
                                $event->start->month,
                                $event->start->mday + 1,
                                $event->start->year));
        }

        $next = mcal_next_recurrence($MCALconnection,
                                     MCAL_SUNDAY,
                                     array('year' => $year,
                                           'month' => $month,
                                           'mday' => $mday));

        // Loop through recurrences while the event:
        // a). Has more recurrences.
        // b). While we are still inside the month.
        while (is_object($next) &&
               isset($next->year) &&
               (mcal_date_compare($next->year,
                                  $next->month,
                                  $next->mday,
                                  $year,
                                  $month,
                                  $daysInMonth) <= 0)) {

            $monthData[$next->mday][$event->id] = $event->title;

            $curday = $next->mday;
            $curmonth = $next->month;
            $curyear = $next->year;
            $next = mcal_next_recurrence($MCALconnection,
                                         MCAL_SUNDAY,
                                         array('year' => $curyear,
                                               'month' => $curmonth,
                                               'mday' => $curday + 1));
        }
    } else {
        // It's not a recurring event, so it must be inside the month.
        $monthData[$event->start->mday][$event->id] = $event->title;
    }
}

// Sort the events by day.
ksort($monthData);

// Print out a crude calendar showing all of the days that have
// events, and each event every time it occurs.
foreach ($monthData as $day => $events) {
    echo $day . ":<br /><ul>";
    foreach ($events as $title) {
        echo "<li>$title</li>";
    }
    echo "</ul>";
}



PHP Functions Essential Reference. Copyright © 2002 by New Riders Publishing (Authors: Zak Greant, Graeme Merrall, Torben Wilson, Brett Michlitsch). This material may be distributed only subject to the terms and conditions set forth in the Open Publication License, v1.0 or later (the latest version is presently available at http://www.opencontent.org/openpub/). The authors of this book have elected not to choose any options under the OPL. This online book was obtained from http://www.fooassociates.com/phpfer/ and is designed to provide information about the PHP programming language, focusing on PHP version 4.0.4 for the most part. The information is provided on an as-is basis, and no warranty or fitness is implied. All persons and entities shall have neither liability nor responsibility to any person or entity with respect to any loss or damage arising from the information contained in this book.

Powered by: vBulletin Version 3.0.7
Copyright ©2000 - 2005, Jelsoft Enterprises Ltd.
Copyright © 2001 - 2005, ThinkDing LLC. All Rights Reserved.