Skip to content

Repository files navigation

PHP ICS Parser

Latest Stable ReleaseTotal Downloads


Installation

Requirements

  • PHP 5 (≥ 5.3.0)
  • Valid ICS (.ics, .ical, .ifb) file
  • IANA, Unicode CLDR or Windows Time Zones
    • Windows will need the $replaceWindowsTimeZoneIds = true configuration

Setup

  • Install Composer
    • Add the following dependency to composer.json
      • ⚠️Note with Composer the owner is johngrogg and not u01jmg3
    • To access the latest stable branch (v2) use the following
      • To access new features you can require dev-master

        {"require": {"johngrogg/ics-parser": "^2"}}

How to use

How to instantiate the Parser

What will the parser return?

  • Each key/value pair from the iCal file will be parsed creating an associative array for both the calendar and every event it contains.

  • Also injected will be content under dtstart_tz and dtend_tz for accessing start and end dates with time zone data applied.

  • Where possible DateTime objects are used and returned.

    // Dump the whole calendarvar_dump($ical->cal);
    // Dump every eventvar_dump($ical->events());
  • Also included are special {property}_array arrays which further resolve the contents of a key/value pair.

    // Dump a parsed event's start datevar_dump($event->dtstart_array);
    // array (size=4)// 0 =>// array (size=1)// 'TZID' => string 'America/Detroit' (length=15)// 1 => string '20160409T090000' (length=15)// 2 => int 1460192400// 3 => string 'TZID=America/Detroit:20160409T090000' (length=36)

API

ICal API

Variables

NameConfigurableDefault ValueDescription
$alarmCount✖️N/ATracks the number of alarms in the current iCal feed
$cal✖️N/AThe parsed calendar
$defaultSpan☑️2The value in years to use for indefinite, recurring events
$defaultTimeZone☑️System defaultEnables customisation of the default time zone
$defaultWeekStart☑️MOThe two letter representation of the first day of the week
$disableCharacterReplacement☑️falseToggles whether to disable all character replacement. Will replace curly quotes and other special characters with their standard equivalents if false. Can be a costly operation!
$eventCount✖️N/ATracks the number of events in the current iCal feed
$filterDaysAfter☑️N/AWhen set the parser will ignore all events more than roughly this many days after now. To be on the safe side it is advised that you make the filter window +/- 1 day larger than necessary. For performance reasons this filter is applied before any date and time zone calculations are done. Hence, depending the time zone settings of the parser and the calendar the cut-off date is not "calibrated". You can then use $ical->eventsFromRange() to precisely shrink the window.
$filterDaysBefore☑️N/AWhen set the parser will ignore all events more than roughly this many days before now. See $filterDaysAfter above for more details.
$freeBusyCount✖️N/ATracks the free/busy count in the current iCal feed
$httpBasicAuth✖️array()Holds the username and password for HTTP basic authentication
$replaceWindowsTimeZoneIds☑️falseToggles whether to replace (non-CLDR) Windows time zone IDs with their IANA equivalent e.g. "Mountain Standard Time" would be replaced with "America/Denver". As there are 130+ Windows time zone IDs that need to be searched and replaced this flag should only be turned on if you know that your calendar file contains such time zone IDs. Microsoft Exchange calendars are often seen using such IDs.
$skipRecurrence☑️falseToggles whether to skip the parsing of recurrence rules
$todoCount✖️N/ATracks the number of todos in the current iCal feed
$useTimeZoneWithRRules☑️falseToggles whether to use time zone info when parsing recurrence rules

Methods

MethodParameter(s)VisibilityDescription
__construct$files = false, $options = array()publicCreates the ICal object
initFile$fileprotectedInitialises lines from a file
initLines$linesprotectedInitialises the parser using an array containing each line of iCal content
initString$stringprotectedInitialises lines from a string
initUrl$url, $username = null, $password = nullprotectedInitialises lines from a URL. Accepts a username/password combination for HTTP basic authentication
addCalendarComponentWithKeyAndValue$component, $keyword, $valueprotectedAdd one key and value pair to the $this->cal array
calendarDescription-publicReturns the calendar description
calendarName-publicReturns the calendar name
calendarTimeZone$ignoreUtcpublicReturns the calendar time zone
cleanData$dataprotectedReplaces curly quotes and other special characters with their standard equivalents
convertDayOrdinalToPositive$dayNumber, $weekday, $timestampprotectedConverts a negative day ordinal to its equivalent positive form
eventsFromInterval$intervalpublicReturns a sorted array of events following a given string, or false if no events exist in the range
eventsFromRange$rangeStart = false, $rangeEnd = falsepublicReturns a sorted array of events in a given range, or an empty array if no events exist in the range
events-publicReturns an array of Events
fileOrUrl$filenameprotectedReads an entire file or URL into an array
freeBusyEvents-publicReturns an array of arrays with all free/busy events
hasEvents-publicReturns a boolean value whether the current calendar has events or not
iCalDateToDateTime$icalDate, $forceTimeZone = false, $forceUtc = falsepublicReturns a DateTime object from an iCal date time format
iCalDateToUnixTimestamp$icalDate, $forceTimeZone = false, $forceUtc = falsepublicReturns a Unix timestamp from an iCal date time format
iCalDateWithTimeZone$event, $key, $format = DATE_TIME_FORMATpublicReturns a date adapted to the calendar time zone depending on the event TZID
isExdateMatch$exdate, $anEvent, $recurringOffsetprotectedChecks if an excluded date matches a given date by reconciling time zones
isFileOrUrl$filenameprotectedChecks if a filename exists as a file or URL
isValidDate$valuepublicChecks if a date string is a valid date
isValidTimeZoneId$timeZoneprotectedChecks if a time zone is valid (IANA or CLDR)
isValidIanaTimeZoneId$timeZoneprotectedChecks if a time zone is a valid IANA time zone
isValidCldrTimeZoneId$timeZone, doConversion = falseprotectedChecks if a time zone is a valid CLDR time zone
keyValueFromString$textprotectedGets the key value pair from an iCal string
mb_chr$codeprotectedProvides a polyfill for PHP 7.2's mb_chr(), which is a multibyte safe version of chr()
mb_str_replace$search, $replace, $subject, $count = 0protectedReplaces all occurrences of a search string with a given replacement string
numberOfDays$days, $start, $endprotectedGets the number of days between a start and end date
parseDuration$date, $duration, $format = 'U'protectedParses a duration and applies it to a date
parseExdates$eventpublicParses a list of excluded dates to be applied to an Event
processDateConversions-protectedProcesses date conversions using the time zone
processEventIcalDateTime$event, $index = 3protectedExtends the {DTSTART|DTEND|RECURRENCE-ID}_array array to include an iCal date time for each event
processEvents-protectedPerforms admin tasks on all events as read from the iCal file
processRecurrences-protectedProcesses recurrence rules
removeUnprintableChars$dataprotectedRemoves unprintable ASCII and UTF-8 characters
sortEventsWithOrder$events, $sortOrder = SORT_ASCpublicSorts events based on a given sort order
trimToRecurrenceCount$rrules, $recurrenceEventsprotectedEnsures the recurrence count is enforced against generated recurrence events
unfold$linesprotectedUnfolds an iCal file in preparation for parsing

Constants

NameDescription
DATE_TIME_FORMAT_PRETTYDefault pretty date time format to use
DATE_TIME_FORMATDefault date time format to use
ICAL_DATE_TIME_TEMPLATEString template to generate an iCal date time
RECURRENCE_EVENTUsed to isolate generated recurrence events
SECONDS_IN_A_WEEKThe number of seconds in a week
TIME_FORMATDefault time format to use
TIME_ZONE_UTCUTC time zone string
UNIX_FORMATUnix timestamp date format
UNIX_MIN_YEARThe year Unix time began

Event API (extends ICal API)

Methods

MethodParameter(s)VisibilityDescription
__construct$data = array()publicCreates the Event object
prepareData$valueprotectedPrepares the data for output
printData$html = HTML_TEMPLATEpublicReturns Event data excluding anything blank within an HTML template
snakeCase$input, $glue = '_', $separator = '-'protectedConverts the given input to snake_case

Constants

NameDescription
HTML_TEMPLATEString template to use when pretty printing content

Credits

  • Jonathan Goode (programming, bug fixing, enhancement, coding standard)
  • John Grogg (programming, addition of event recurrence handling)

Tools for Testing

About

Parser for iCalendar Events • written in PHP 5 (≥ 5.3.0)

Resources

Contributing

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages