Create a PHP script that can read a CSV in the form start_date, end_date, event and output as ICS file
function convertDate($date) { $dateTime = DateTime::createFromFormat('m/d/Y', $date); if ($dateTime === false) { return false; // Return false if date parsing fails } return $dateTime->format('Ymd'); } // Function to escape special characters in text function escapeText($text) { return str_replace(["\n", "\r", ",", ";"], ['\n', '\r', '\,', '\;'], $text); } // Read CSV file $csvFile = 'uci.csv'; // Replace with your CSV file name $icsFile = 'uci.ics'; // Output ICS file name $handle = fopen($csvFile, 'r'); if ($handle !== false) { // Open ICS file for writing $icsHandle = fopen($icsFile, 'w'); // Write ICS header fwrite($icsHandle, "BEGIN:VCALENDAR\r\n"); fwrite($icsHandle, "VERSION:2.0\r\n"); fwrite($icsHandle, "PRODID:-//Your Company//NONSGML Event Calendar//EN\r\n"); // Read CSV line by line while (($data = fgetcsv($handle, 1000, ',')) !== false) { $startDate = convertDate($data[0]); $endDate = convertDate($data[1]); print_r($data) . PHP_EOL; echo $startDate; if ($startDate === false || $endDate === false) { continue; } $event = escapeText($data[2]); // Write event to ICS file fwrite($icsHandle, "BEGIN:VEVENT\r\n"); fwrite($icsHandle, "UID:" . uniqid() . "\r\n"); // Unique identifier fwrite($icsHandle, "DTSTART;VALUE=DATE:" . $startDate . "\r\n"); fwrite($icsHandle, "DTEND;VALUE=DATE:" . $endDate . "\r\n"); fwrite($icsHandle, "SUMMARY:" . $event . "\r\n"); fwrite($icsHandle, "DESCRIPTION:" . $event . "\r\n"); fwrite($icsHandle, "END:VEVENT\r\n"); } // Write ICS footer fwrite($icsHandle, "END:VCALENDAR\r\n"); // Close files fclose($icsHandle); fclose($handle); echo "ICS file generated successfully."; } else { echo "Error: Unable to open CSV file."; }
Source data are from UCI and output is here from where it can be added as a calendar. BTW created also my first “hello world” Swift/iPhone app using this source although this took a bit more time…