項(xiàng)目用到了,百度沒找到相關(guān)的答案,后來在github上的gist找到了代碼。點(diǎn)擊查看 (注:不開VPN的話打不開)
源碼只支持生成一個(gè)事件的ics,我現(xiàn)在改成了支持生成多事件(支持傳入多維數(shù)組)。
文件開頭的注釋部分有使用例子。
<?php/** * ICS.php * ======= * Use this class to create an .ics file. * * Usage * ----- * Basic usage - generate ics file contents (see below for available PRoperties): * $ics = new ICS($props); * $ics_file_contents = $ics->to_string(); * * Setting properties after instantiation * $ics = new ICS(); * $ics->set('summary', 'My awesome event'); * * You can also set: * $ics->set(array( * array( * 'dtstart' => 'now + 30 minutes', * 'dtend' => 'now + 1 hour', * ), * array( * 'dtstart' => 'now + 40 minutes', * 'dtend' => 'now + 2 hour', * ), * )); * * Available properties * -------------------- * description * String description of the event. * dtend * A date/time stamp designating the end of the event. You can use either a * DateTime object or a PHP datetime format string (e.g. "now + 1 hour"). * dtstart * A date/time stamp designating the start of the event. You can use either a * DateTime object or a PHP datetime format string (e.g. "now + 1 hour"). * location * String address or description of the location of the event. * summary * String short summary of the event - usually used as the title. * url * A url to attach to the the event. Make sure to add the protocol (http:// * or https://). */class ICS{ const DT_FORMAT = 'Ymd/THis/Z'; protected $properties = array(); private $available_properties = array( 'description', 'dtend', 'dtstart', 'location', 'summary', 'url' ); public function __construct($props) { $this->set($props); } public function set($key, $val = false, $num = 0) { if (is_array($key)) { foreach ($key as $k => $v) { $this->set($k, $v); } } elseif(is_array($val)) { foreach ($val as $k => $v) { $this->set($k, $v, $key); } } else { if (in_array($key, $this->available_properties)) { if(!is_array($this->properties[$num])) { $this->properties[$num] = []; } $this->properties[$num][$key] = $this->sanitize_val($val, $key); } } } public function to_string() { $rows = $this->build_props(); return implode("/r/n", $rows); } private function build_props() { // Build ICS properties - add header $ics_props = array( 'BEGIN:VCALENDAR', 'VERSION:2.0', 'PRODID:-//hacksw/handcal//NONSGML v1.0//EN', 'CALSCALE:GREGORIAN', ); foreach($this->properties as $key => $val) { $ics_props[] = 'BEGIN:VEVENT'; foreach ($val as $k => $v) { $ics_props[] = strtoupper($k . ($k === 'url' ? ';VALUE=URI' : '')).':'.$v; } $ics_props[] = 'DTSTAMP:'.$this->format_timestamp('now'); $ics_props[] = 'UID:'.uniqid(); $ics_props[] = 'END:VEVENT'; } $ics_props[] = 'END:VCALENDAR'; return $ics_props; } private function sanitize_val($val, $key = false) { switch ($key) { case 'dtend': case 'dtstamp': case 'dtstart': $val = $this->format_timestamp($val); break; default: $val = $this->escape_string($val); } return $val; } private function format_timestamp($timestamp) { $dt = new /DateTime($timestamp); return $dt->format(self::DT_FORMAT); } private function escape_string($str) { return preg_replace('/([/,;])/', '///$1', $str); }}
新聞熱點(diǎn)
疑難解答