国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > PHP > 正文

php 生成ics文件

2019-11-11 00:03:29
字體:
供稿:網(wǎng)友

項(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);	}}


發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 荔波县| 吴忠市| 龙江县| 南和县| 大竹县| 德钦县| 永年县| 且末县| 漾濞| 丹凤县| 望奎县| 五台县| 策勒县| 秭归县| 长岭县| 兴海县| 横峰县| 互助| 兴宁市| 恭城| 肇州县| 岳阳市| 天柱县| 长子县| 炎陵县| 台南县| 张北县| 尚志市| 建湖县| 塔河县| 赤峰市| 蓬安县| 常德市| 永德县| 陕西省| 安仁县| 武邑县| 临武县| 运城市| 巴中市| 屏东市|