Are there any best practices or libraries recommended for creating ics files in PHP to avoid common pitfalls like incorrect timezones?

When creating ics files in PHP, it is important to ensure that the timezones are correctly set to avoid common pitfalls. One recommended best practice is to use the PHP library called "johngrogg/ics-parser" which handles timezones automatically and helps in creating valid ics files.

// Include the library
require_once 'vendor/autoload.php';

use Eluceo\iCal\Component\Calendar;
use Eluceo\iCal\Component\Event;
use Eluceo\iCal\Property\Event\RecurrenceRule;

// Create a new calendar instance
$calendar = new Calendar('www.example.com');

// Create a new event
$event = new Event();
$event->setDtStart(new \DateTime('2023-01-01 12:00:00', new \DateTimeZone('America/New_York')));
$event->setDtEnd(new \DateTime('2023-01-01 13:00:00', new \DateTimeZone('America/New_York')));
$event->setSummary('Example Event');
$event->setDescription('This is an example event.');

// Add the event to the calendar
$calendar->addComponent($event);

// Output the calendar
header('Content-Type: text/calendar; charset=utf-8');
header('Content-Disposition: attachment; filename="example.ics"');

echo $calendar->render();