What is the best approach to filter and display specific events from an internal section to an external calendar in PHP?

To filter and display specific events from an internal section to an external calendar in PHP, you can create a script that fetches the relevant events from your internal database or source, formats them in a compatible calendar format (such as iCal or Google Calendar), and then provides a link or feed for users to subscribe to in their external calendar application.

<?php

// Fetch events from internal database
$events = array(
    array(
        'title' => 'Event 1',
        'start' => '2022-01-01',
        'end' => '2022-01-02',
        'description' => 'This is event 1'
    ),
    array(
        'title' => 'Event 2',
        'start' => '2022-02-01',
        'end' => '2022-02-02',
        'description' => 'This is event 2'
    )
);

// Generate iCal format for events
$ical = "BEGIN:VCALENDAR\n";
$ical .= "VERSION:2.0\n";
foreach ($events as $event) {
    $ical .= "BEGIN:VEVENT\n";
    $ical .= "DTSTART:" . date('Ymd\THis', strtotime($event['start'])) . "\n";
    $ical .= "DTEND:" . date('Ymd\THis', strtotime($event['end'])) . "\n";
    $ical .= "SUMMARY:" . $event['title'] . "\n";
    $ical .= "DESCRIPTION:" . $event['description'] . "\n";
    $ical .= "END:VEVENT\n";
}
$ical .= "END:VCALENDAR";

// Output the iCal feed
header('Content-type: text/calendar; charset=utf-8');
header('Content-Disposition: attachment; filename=events.ics');
echo $ical;