Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
981 views
in Technique[技术] by (71.8m points)

parsing - ICalendar parser in PHP that supports timezones

I am looking for a PHP class that can parse an ICalendar (ICS) file and correctly handle timezones.

I already created an ICS parser myself but it can only handle timezones known to PHP (like 'Europe/Paris').

Unfortunately, ICS file generated by Evolution (default calendar software of Ubuntu) does not use default timezone IDs. It exports events with its a specific timezone ID exporting also the full definition of the timezone: daylight saving dates, recurrence rule and all the hard stuff to understand about timezones.

This is too much for me. Since it was only a small utility for my girlfriend, I won't have time to investigate further the ICalendar specification and create a full blown ICalendar parser myself.

So is there any known implementation in PHP of ICalendar file format that can parse timezones definitions?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Most likely there are a lot of libraries that parse .ics files, but I'll show you one example that works for me quite well.

I've used this library: http://www.phpclasses.org/browse/file/16660.html

It gives you a lot of flexibility in handling different types of ICal components: VEVENT, VTODO, VJOURNAL, VFREEBUSY, VALARM, and VTIMEZONE (the one you were asking about).

Example:

<pre><?php

//
// Open library
//
require_once( "iCalcreator.class.php" ) ;

//
// Demo ICal file contents
//
$string = <<<EOS
BEGIN:VCALENDAR
VERSION:2.0
PRODID:-//hacksw/handcal//NONSGML v1.0//EN
BEGIN:VTIMEZONE
TZID:US-Eastern
LAST-MODIFIED:19870101T000000Z
BEGIN:STANDARD
DTSTART:19971026T020000
RDATE:19971026T020000
TZOFFSETFROM:-0400
TZOFFSETTO:-0500
TZNAME:EST
END:STANDARD
BEGIN:DAYLIGHT
DTSTART:19971026T020000
RDATE:19970406T020000
TZOFFSETFROM:-0500
TZOFFSETTO:-0400
TZNAME:EDT
END:DAYLIGHT
END:VTIMEZONE
END:VCALENDAR
EOS
;

//
// There is no direct string parsing functionality,
// so first create a temporary file
//
$filename = tempnam( ".", "" ) ;
$f = fopen($filename,"w") ;
fwrite( $f, $string );
fclose($f);

//
// ... parse it into an object
//
$var = new vcalendar();
$var->parse($filename);
var_dump( $var );
$event = $var->components[0] ;
var_dump( $event->createDtstamp() );


//
// ... and finally remove all temporary data.
//
unlink($filename);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...