src/ICal/ICal.php
public function eventsFromRange($rangeStart = null, $rangeEnd = null)
    {
        // Sort events before processing range
        $events = $this->sortEventsWithOrder($this->events(), SORT_ASC);

        if (empty($events)) {
            return array();
        }

        $extendedEvents = array();

        if (!is_null($rangeStart)) {
            try {
                $rangeStart = new \DateTime($rangeStart, new \DateTimeZone($this->defaultTimeZone));
            } catch (\Exception $e) {
                error_log("ICal::eventsFromRange: Invalid date passed ({$rangeStart})");
                $rangeStart = false;
            }
        } else {
            $rangeStart = new \DateTime('now', new \DateTimeZone($this->defaultTimeZone));
        }

        if (!is_null($rangeEnd)) {
            try {
                $rangeEnd = new \DateTime($rangeEnd, new \DateTimeZone($this->defaultTimeZone));
            } catch (\Exception $e) {
                error_log("ICal::eventsFromRange: Invalid date passed ({$rangeEnd})");
                $rangeEnd = false;
            }
        } else {
            $rangeEnd = new \DateTime('now', new \DateTimeZone($this->defaultTimeZone));
            $rangeEnd->modify('+20 years');
        }

        // If start and end are identical and are dates with no times...
        if ($rangeEnd->format('His') == 0 && $rangeStart->getTimestamp() == $rangeEnd->getTimestamp()) {
            $rangeEnd->modify('+1 day');
        }

        $rangeStart = $rangeStart->getTimestamp();
        $rangeEnd   = $rangeEnd->getTimestamp();

        foreach ($events as $anEvent) {
            $eventStart = $anEvent->dtstart_array[2];
            $eventEnd   = (isset($anEvent->dtend_array[2])) ? $anEvent->dtend_array[2] : null;

            if (($eventStart >= $rangeStart && $eventStart < $rangeEnd)         // Event start date contained in the range
                || ($eventEnd !== null
                    && (
                        ($eventEnd > $rangeStart && $eventEnd <= $rangeEnd)     // Event end date contained in the range
                        || ($eventStart < $rangeStart && $eventEnd > $rangeEnd) // Event starts before and finishes after range
                    )
                )
            ) {
                $extendedEvents[] = $anEvent;
            }
        }

        if (empty($extendedEvents)) {
            return array();
        }

        return $extendedEvents;
    }
public function eventsFromRange($rangeStart = null, $rangeEnd = null)
    {
        // Sort events before processing range
        $events = $this->sortEventsWithOrder($this->events(), SORT_ASC);

        if (empty($events)) {
            return array();
        }

        $extendedEvents = array();

        if (!is_null($rangeStart)) {
            try {
                $rangeStart = new \DateTime($rangeStart, new \DateTimeZone($this->defaultTimeZone));
            } catch (\Exception $e) {
                error_log("ICal::eventsFromRange: Invalid date passed ({$rangeStart})");
                $rangeStart = false;
            }
        } else {
            $rangeStart = new \DateTime('now', new \DateTimeZone($this->defaultTimeZone));
        }

        if (!is_null($rangeEnd)) {
            try {
                $rangeEnd = new \DateTime($rangeEnd, new \DateTimeZone($this->defaultTimeZone));
            } catch (\Exception $e) {
                error_log("ICal::eventsFromRange: Invalid date passed ({$rangeEnd})");
                $rangeEnd = false;
            }
        } else {
            $rangeEnd = new \DateTime('now', new \DateTimeZone($this->defaultTimeZone));
            $rangeEnd->modify('+20 years');
        }

        // If start and end are identical and are dates with no times...
        if ($rangeEnd->format('His') == 0 && $rangeStart->getTimestamp() == $rangeEnd->getTimestamp()) {
            $rangeEnd->modify('+1 day');
        }

        $rangeStart = $rangeStart->getTimestamp();
        $rangeEnd   = $rangeEnd->getTimestamp();

        foreach ($events as $anEvent) {
            $eventStart = $anEvent->dtstart_array[2];
            $eventEnd   = (isset($anEvent->dtend_array[2])) ? $anEvent->dtend_array[2] : null;

            if (($eventStart >= $rangeStart && $eventStart < $rangeEnd)         // Event start date contained in the range
                || ($eventEnd !== null
                    && (
                        ($eventEnd > $rangeStart && $eventEnd <= $rangeEnd)     // Event end date contained in the range
                        || ($eventStart < $rangeStart && $eventEnd > $rangeEnd) // Event starts before and finishes after range
                    )
                )
            ) {
                $extendedEvents[] = $anEvent;
            }
        }

        if (empty($extendedEvents)) {
            return array();
        }

        return $extendedEvents;
    }
public function eventsFromRange ( $ rangeStart = null , $ rangeEnd = null ) { // Sort events before processing range $ events = $ this -> sortEventsWithOrder ( $ this -> events ( ) , SORT_ASC ) ; if ( empty ( $ events ) ) { return array ( ) ; } $ extendedEvents = array ( ) ; if ( ! is_null ( $ rangeStart ) ) { try { $ rangeStart = new \ DateTime ( $ rangeStart , new \ DateTimeZone ( $ this -> defaultTimeZone ) ) ; } catch ( \ Exception $ e ) { error_log ( "ICal::eventsFromRange: Invalid date passed ({$rangeStart})" ) ; $ rangeStart = false ; } } else { $ rangeStart = new \ DateTime ( 'now' , new \ DateTimeZone ( $ this -> defaultTimeZone ) ) ; } if ( ! is_null ( $ rangeEnd ) ) { try { $ rangeEnd = new \ DateTime ( $ rangeEnd , new \ DateTimeZone ( $ this -> defaultTimeZone ) ) ; } catch ( \ Exception $ e ) { error_log ( "ICal::eventsFromRange: Invalid date passed ({$rangeEnd})" ) ; $ rangeEnd = false ; } } else { $ rangeEnd = new \ DateTime ( 'now' , new \ DateTimeZone ( $ this -> defaultTimeZone ) ) ; $ rangeEnd -> modify ( '+20 years' ) ; } // If start and end are identical and are dates with no times... if ( $ rangeEnd -> format ( 'His' ) == 0 && $ rangeStart -> getTimestamp ( ) == $ rangeEnd -> getTimestamp ( ) ) { $ rangeEnd -> modify ( '+1 day' ) ; } $ rangeStart = $ rangeStart -> getTimestamp ( ) ; $ rangeEnd = $ rangeEnd -> getTimestamp ( ) ; foreach ( $ events as $ anEvent ) { $ eventStart = $ anEvent -> dtstart_array [ 2 ] ; $ eventEnd = ( isset ( $ anEvent -> dtend_array [ 2 ] ) ) ? $ anEvent -> dtend_array [ 2 ] : null ; if ( ( $ eventStart >= $ rangeStart && $ eventStart < $ rangeEnd ) // Event start date contained in the range || ( $ eventEnd !== null && ( ( $ eventEnd > $ rangeStart && $ eventEnd <= $ rangeEnd ) // Event end date contained in the range || ( $ eventStart < $ rangeStart && $ eventEnd > $ rangeEnd ) // Event starts before and finishes after range ) ) ) { $ extendedEvents [ ] = $ anEvent ; } } if ( empty ( $ extendedEvents ) ) { return array ( ) ; } return $ extendedEvents ; }
Returns a sorted array of the events in a given range,
or an empty array if no events exist in the range.

Events will be returned if the start or end date is contained within the
range (inclusive), or if the event starts before and end after the range.

If a start date is not specified or of a valid format, then the start
of the range will default to the current time and date of the server.

If an end date is not specified or of a valid format, then the end of
the range will default to the current time and date of the server,
plus 20 years.

Note that this function makes use of Unix timestamps. This might be a
problem for events on, during, or after 29 Jan 2038.
See https://en.wikipedia.org/wiki/Unix_time#Representing_the_number

@param  string|null $rangeStart
@param  string|null $rangeEnd
@return array
@throws \Exception
Returns a sorted array of the events in a given range or an empty array if no events exist in the range .