Path: blob/master/web-gui/buildyourownbotnet/assets/js/fullcalendar-2/demos/php/utils.php
1296 views
<?php12//--------------------------------------------------------------------------------------------------3// Utilities for our event-fetching scripts.4//5// Requires PHP 5.2.0 or higher.6//--------------------------------------------------------------------------------------------------78// PHP will fatal error if we attempt to use the DateTime class without this being set.9date_default_timezone_set('UTC');101112class Event {1314// Tests whether the given ISO8601 string has a time-of-day or not15const ALL_DAY_REGEX = '/^\d{4}-\d\d-\d\d$/'; // matches strings like "2013-12-29"1617public $title;18public $allDay; // a boolean19public $start; // a DateTime20public $end; // a DateTime, or null21public $properties = array(); // an array of other misc properties222324// Constructs an Event object from the given array of key=>values.25// You can optionally force the timezone of the parsed dates.26public function __construct($array, $timezone=null) {2728$this->title = $array['title'];2930if (isset($array['allDay'])) {31// allDay has been explicitly specified32$this->allDay = (bool)$array['allDay'];33}34else {35// Guess allDay based off of ISO8601 date strings36$this->allDay = preg_match(self::ALL_DAY_REGEX, $array['start']) &&37(!isset($array['end']) || preg_match(self::ALL_DAY_REGEX, $array['end']));38}3940if ($this->allDay) {41// If dates are allDay, we want to parse them in UTC to avoid DST issues.42$timezone = null;43}4445// Parse dates46$this->start = parseDateTime($array['start'], $timezone);47$this->end = isset($array['end']) ? parseDateTime($array['end'], $timezone) : null;4849// Record misc properties50foreach ($array as $name => $value) {51if (!in_array($name, array('title', 'allDay', 'start', 'end'))) {52$this->properties[$name] = $value;53}54}55}565758// Returns whether the date range of our event intersects with the given all-day range.59// $rangeStart and $rangeEnd are assumed to be dates in UTC with 00:00:00 time.60public function isWithinDayRange($rangeStart, $rangeEnd) {6162// Normalize our event's dates for comparison with the all-day range.63$eventStart = stripTime($this->start);64$eventEnd = isset($this->end) ? stripTime($this->end) : null;6566if (!$eventEnd) {67// No end time? Only check if the start is within range.68return $eventStart < $rangeEnd && $eventStart >= $rangeStart;69}70else {71// Check if the two ranges intersect.72return $eventStart < $rangeEnd && $eventEnd > $rangeStart;73}74}757677// Converts this Event object back to a plain data array, to be used for generating JSON78public function toArray() {7980// Start with the misc properties (don't worry, PHP won't affect the original array)81$array = $this->properties;8283$array['title'] = $this->title;8485// Figure out the date format. This essentially encodes allDay into the date string.86if ($this->allDay) {87$format = 'Y-m-d'; // output like "2013-12-29"88}89else {90$format = 'c'; // full ISO8601 output, like "2013-12-29T09:00:00+08:00"91}9293// Serialize dates into strings94$array['start'] = $this->start->format($format);95if (isset($this->end)) {96$array['end'] = $this->end->format($format);97}9899return $array;100}101102}103104105// Date Utilities106//----------------------------------------------------------------------------------------------107108109// Parses a string into a DateTime object, optionally forced into the given timezone.110function parseDateTime($string, $timezone=null) {111$date = new DateTime(112$string,113$timezone ? $timezone : new DateTimeZone('UTC')114// Used only when the string is ambiguous.115// Ignored if string has a timezone offset in it.116);117if ($timezone) {118// If our timezone was ignored above, force it.119$date->setTimezone($timezone);120}121return $date;122}123124125// Takes the year/month/date values of the given DateTime and converts them to a new DateTime,126// but in UTC.127function stripTime($datetime) {128return new DateTime($datetime->format('Y-m-d'));129}130131132