tinuviel/php-ics

在PHP中生成iCalendar (.ics)文件。

0.3.0 2017-11-24 16:02 UTC

This package is not auto-updated.

Last update: 2024-09-29 05:14:42 UTC


README

此代码片段包含一个方便的脚本来在PHP中动态生成iCalendar (.ics)文件。

基本用法

include 'ICS.php'

$properties = array(
  'dtstart' => 'now',
  'dtend' => 'now + 30 minutes'
);

$ics = new ICS($properties);
$ics_file_contents = $ics->to_string();

可用属性

  • description - 事件字符串描述。
  • dtend - 指定事件结束的日期/时间戳。您可以使用一个 DateTime 对象或一个 PHP datetime格式字符串(例如:"now + 1 hour")。
  • dtstart - 指定事件开始的日期/时间戳。您可以使用一个 DateTime 对象或一个 PHP datetime格式字符串(例如:"now + 1 hour")。
  • location - 字符串形式的地址或事件位置的描述。
  • summary - 事件的字符串简短摘要 - 通常用作标题。
  • url - 附加到事件的字符串url。请确保添加协议(http://https://)。

详细示例

点击时下载ICS文件的按钮

此示例包含一个前端表单,该表单提交到PHP脚本,该脚本启动ICS文件的下载。此示例使用隐藏表单字段来动态设置属性。

index.html

<form method="post" action="/download-ics.php">
  <input type="hidden" name="date_start" value="2017-1-16 9:00AM">
  <input type="hidden" name="date_end" value="2017-1-16 10:00AM">
  <input type="hidden" name="location" value="123 Fake St, New York, NY">
  <input type="hidden" name="description" value="This is my description">
  <input type="hidden" name="summary" value="This is my summary">
  <input type="hidden" name="url" value="http://example.com">
  <input type="submit" value="Add to Calendar">
</form>

download-ics.php

<?php

include 'ICS.php';

header('Content-type: text/calendar; charset=utf-8');
header('Content-Disposition: attachment; filename=invite.ics');

$ics = new ICS(array(
  'location' => $_POST['location'],
  'description' => $_POST['description'],
  'dtstart' => $_POST['date_start'],
  'dtend' => $_POST['date_end'],
  'summary' => $_POST['summary'],
  'url' => $_POST['url']
));

echo $ics->to_string();