首页 > 编程 > PHP > 正文

php 生成ics文件

2019-11-11 01:38:39
字体:
来源:转载
供稿:网友

项目用到了,百度没找到相关的答案,后来在github上的gist找到了代码。点击查看 (注:不开VPN的话打不开)

源码只支持生成一个事件的ics,我现在改成了支持生成多事件(支持传入多维数组)。

文件开头的注释部分有使用例子。

<?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);	}}


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表