PluginProbe
Booking Manager – Sync WP Booking Calendar – Import Events, Export Bookings to ICS Calendar / 2.1.21
Booking Manager – Sync WP Booking Calendar – Import Events, Export Bookings to ICS Calendar v2.1.21
2.1.21 2.1.20 2.1.4 2.1.5 2.1.6 2.1.7 2.1.8 2.1.9 trunk 1.1 2.0 2.0.1 2.0.10.2 2.0.11 2.0.12 2.0.13 2.0.14 2.0.15 2.0.16 2.0.17 2.0.18 2.0.2 2.0.20 2.0.21 2.0.22 All 55 releases
booking-manager / assets / libs / icalendar / includes / date.php

date.php in Booking Manager – Sync WP Booking Calendar – Import Events, Export Bookings to ICS Calendar 2.1.21, at assets/libs/icalendar/includes/date.php

565 lines 15.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * Zap Calendar Date Helper Class
5 *
6 * @copyright Copyright (C) 2006 - 2016 by Dan Cogliano
7 * @license GNU General Public License version 2 or later; see LICENSE.txt
8 */
9
10 // No direct access
11 defined('_ZAPCAL') or die( 'Restricted access' );
12
13 /**
14 * Helper class for various date functions
15 */
16 class ZDateHelper {
17
18 /**
19 * Find the number of days in a month
20 *
21 * @param int $month Month is between 1 and 12 inclusive
22 *
23 * @param int $year is between 1 and 32767 inclusive
24 *
25 * @return int
26 */
27 static function DayInMonth($month, $year) {
28 $daysInMonth = array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
29 if ($month != 2) return $daysInMonth[$month - 1];
30 return (checkdate($month, 29, $year)) ? 29 : 28;
31 }
32
33 /**
34 * Is given date today?
35 *
36 * @param int $date date in Unix timestamp format
37 *
38 * @param int $tzid PHP recognized timezone (default is UTC)
39 *
40 * @return bool
41 */
42 static function isToday($date, $tzid = "UTC") {
43 $dtz = new DateTimeZone($tzid);
44 $dt = new DateTime("now", $dtz);
45 $now = time() + $dtz->getOffset($dt);
46 return gmdate('Y-m-d', $date) == gmdate('Y-m-d', $now);
47 }
48
49 /**
50 * Is given date before today?
51 *
52 * @param int $date date in Unix timestamp format
53 *
54 * @param int $tzid PHP recognized timezone (default is UTC)
55 *
56 * @return bool
57 */
58 static function isBeforeToday($date, $tzid = "UTC"){
59 $dtz = new DateTimeZone($tzid);
60 $dt = new DateTime("now", $dtz);
61 $now = time() + $dtz->getOffset($dt);
62 return mktime(0,0,0,date('m',$now),date('d',$now),date('Y',$now)) >
63 mktime(0,0,0,date('m',$date),date('d',$date),date('Y',$now));
64 }
65
66 /**
67 * Is given date after today?
68 *
69 * @param int $date date in Unix timestamp format
70 *
71 * @param int $tzid PHP recognized timezone (default is UTC)
72 *
73 * @return bool
74 */
75 static function isAfterToday($date, $tzid = "UTC"){
76 $dtz = new DateTimeZone($tzid);
77 $dt = new DateTime("now", $dtz);
78 $now = time() + $dtz->getOffset($dt);
79 return mktime(0,0,0,date('m',$now),date('d',$now),date('Y',$now)) <
80 mktime(0,0,0,date('m',$date),date('d',$date),date('Y',$now));
81 }
82
83 /**
84 * Is given date tomorrow?
85 *
86 * @param int $date date in Unix timestamp format
87 *
88 * @param int $tzid PHP recognized timezone (default is UTC)
89 *
90 * @return bool
91 */
92 static function isTomorrow($date, $tzid = "UTC") {
93 $dtz = new DateTimeZone($tzid);
94 $dt = new DateTime("now", $dtz);
95 $now = time() + $dtz->getOffset($dt);
96 return gmdate('Y-m-d', $date) == gmdate('Y-m-d', $now + 60 * 60 * 24);
97 }
98
99 /**
100 * Is given date in the future?
101 *
102 * This routine differs from isAfterToday() in that isFuture() will
103 * return true for date-time values later in the same day.
104 *
105 * @param int $date date in Unix timestamp format
106 *
107 * @param int $tzid PHP recognized timezone (default is UTC)
108 *
109 * @return bool
110 */
111 static function isFuture($date, $tzid = "UTC"){
112 $dtz = new DateTimeZone($tzid);
113 $dt = new DateTime("now", $dtz);
114 $now = time() + $dtz->getOffset($dt);
115 return $date > $now;
116 }
117
118 /**
119 * Is given date in the past?
120 *
121 * This routine differs from isBeforeToday() in that isPast() will
122 * return true for date-time values earlier in the same day.
123 *
124 * @param int $date date in Unix timestamp format
125 *
126 * @param int $tzid PHP recognized timezone (default is UTC)
127 *
128 * @return bool
129 */
130 static function isPast($date, $tzid = "UTC") {
131 $dtz = new DateTimeZone($tzid);
132 $dt = new DateTime("now", $dtz);
133 $now = time() + $dtz->getOffset($dt);
134 return $date < $now;
135 }
136
137 /**
138 * Return current Unix timestamp in local timezone
139 *
140 * @param string $tzid PHP recognized timezone
141 *
142 * @return int
143 */
144 static function now($tzid = "UTC"){
145 $dtz = new DateTimeZone($tzid);
146 $dt = new DateTime("now", $dtz);
147 $now = time() + $dtz->getOffset($dt);
148 return $now;
149 }
150
151 /**
152 * Is given date fall on a weekend?
153 *
154 * @param int $date Unix timestamp
155 *
156 * @return bool
157 */
158 static function isWeekend($date) {
159 $dow = gmdate('w',$date);
160 return $dow == 0 || $dow == 6;
161 }
162
163 /**
164 * Format Unix timestamp to SQL date-time
165 *
166 * @param int $t Unix timestamp
167 *
168 * @return string
169 */
170 static function toSqlDateTime($t = 0)
171 {
172 date_default_timezone_set('GMT');
173 if($t == 0)
174 return gmdate('Y-m-d H:i:s',self::now());
175 return gmdate('Y-m-d H:i:s', $t);
176 }
177
178 /**
179 * Format Unix timestamp to SQL date
180 *
181 * @param int $t Unix timestamp
182 *
183 * @return string
184 */
185 static function toSqlDate($t = 0)
186 {
187 date_default_timezone_set('GMT');
188 if($t == 0)
189 return gmdate('Y-m-d',self::now());
190 return gmdate('Y-m-d', $t);
191 }
192
193 /**
194 * Format iCal date-time string to Unix timestamp
195 *
196 * @param string $datetime in iCal time format ( YYYYMMDD or YYYYMMDDTHHMMSS or YYYYMMDDTHHMMSSZ )
197 *
198 * @return int Unix timestamp
199 */
200 static function fromiCaltoUnixDateTime($datetime) {
201 // first check format
202 $formats = array();
203 $formats[] = "/[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]/";
204 $formats[] = "/[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]T[0-9][0-9][0-9][0-9][0-9][0-9]/";
205 $formats[] = "/[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]T[0-9][0-9][0-9][0-9][0-9][0-9]Z/";
206 $ok = false;
207 foreach($formats as $format){
208 if(preg_match($format,$datetime)){
209 $ok = true;
210 break;
211 }
212 }
213 if(!$ok)
214 return null;
215 $year = substr($datetime,0,4);
216 $month = substr($datetime,4,2);
217 $day = substr($datetime,6,2);
218 $hour = 0;
219 $minute = 0;
220 $second = 0;
221 if(strlen($datetime) > 8 && $datetime[8] == "T") { //FixIn: 2.0.17.1
222 $hour = substr($datetime,9,2);
223 $minute = substr($datetime,11,2);
224 $second = substr($datetime,13,2);
225 }
226 return gmmktime($hour, $minute, $second, $month, $day, $year);
227 }
228
229 /**
230 * Format Unix timestamp to iCal date-time string
231 *
232 * @param int $datetime Unix timestamp
233 *
234 * @return string
235 */
236 static function fromUnixDateTimetoiCal($datetime){
237 date_default_timezone_set('GMT');
238 return gmdate("Ymd\THis",$datetime);
239 }
240
241 /**
242 * Convert iCal duration string to # of seconds
243 *
244 * @param string $duration iCal duration string
245 *
246 * return int
247 */
248 static function iCalDurationtoSeconds($duration) {
249 $secs = 0;
250 if($duration[0] == "P") { //FixIn: 2.0.17.1
251 $duration = str_replace(array("H","M","S","T","D","W","P"),array("H,","M,","S,","","D,","W,",""),$duration);
252 $dur2 = explode(",",$duration);
253 foreach($dur2 as $dur){
254 $val=intval($dur);
255 if(strlen($dur) > 0){
256 switch($dur[strlen($dur) - 1]) { //FixIn: 2.0.17.1
257 case "H":
258 $secs += 60*60 * $val;
259 break;
260 case "M":
261 $secs += 60 * $val;
262 break;
263 case "S":
264 $secs += $val;
265 break;
266 case "D":
267 $secs += 60*60*24 * $val;
268 break;
269 case "W":
270 $secs += 60*60*24*7 * $val;
271 break;
272 }
273 }
274 }
275 }
276 return $secs;
277 }
278
279 /**
280 * Check if day falls within date range
281 *
282 * @param int $daystart start of day in Unix timestamp format
283 *
284 * @param int $begin Unix timestamp of starting date range
285 *
286 * @param int $end Unix timestamp of end date range
287 *
288 * @return bool
289 */
290 static function inDay($daystart, $begin, $end)
291 {
292 //$dayend = $daystart + 60*60*24 - 60;
293 // add 1 day to determine end of day
294 // don't use 24 hours, since twice a year DST Sundays are 23 hours and 25 hours in length
295 // adding 1 day takes this into account
296 $dayend = self::addDate($daystart, 0,0,0,0,1,0);
297
298 $end = max($begin, $end); // $end can't be less than $begin
299 $inday =
300 ($daystart <= $begin && $begin < $dayend)
301 ||($daystart < $end && $end < $dayend)
302 ||($begin <= $daystart && $end > $dayend)
303 ;
304 return $inday;
305 }
306
307 /**
308 * Convert SQL date or date-time to Unix timestamp
309 *
310 * @param string $datetime SQL date or date-time
311 *
312 * @return int Unix date-time timestamp
313 */
314 static function toUnixDate($datetime)
315 {
316 $year = substr($datetime,0,4);
317 $month = substr($datetime,5,2);
318 $day = substr($datetime,8,2);
319
320 return mktime(0, 0, 0, $month, $day, $year);
321 }
322
323 /**
324 * Convert SQL date or date-time to Unix date timestamp
325 *
326 * @param string $datetime SQL date or date-time
327 *
328 * @return int Unix timestamp
329 */
330 static function toUnixDateTime($datetime)
331 {
332 // convert to absolute dates if neccessary
333 $datetime = self::getAbsDate($datetime);
334 $year = substr($datetime,0,4);
335 $month = substr($datetime,5,2);
336 $day = substr($datetime,8,2);
337 $hour = 0;
338 $minute = 0;
339 $second = 0;
340 if(strlen($datetime) > 10) {
341 $hour = substr($datetime,11,2);
342 $minute = substr($datetime,14,2);
343 $second = substr($datetime,17,2);
344 }
345 return gmmktime($hour, $minute, $second, $month, $day, $year);
346 }
347
348 /**
349 * Date math: add or substract from current date to get a new date
350 *
351 * @param int $date date to add or subtract from
352 *
353 * @param int $hour add or subtract hours from date
354 *
355 * @param int $min add or subtract minutes from date
356 *
357 * @param int $sec add or subtract seconds from date
358 *
359 * @param int $month add or subtract months from date
360 *
361 * @param int $day add or subtract days from date
362 *
363 * @param int $year add or subtract years from date
364 *
365 * @param string $tzid PHP recognized timezone (default is UTC)
366 */
367 static function addDate($date, $hour, $min, $sec, $month, $day, $year, $tzid = "UTC") {
368 date_default_timezone_set($tzid);
369 $sqldate = self::toSQLDateTime($date);
370 $tdate = array();
371 $tdate["year"] = substr($sqldate,0,4);
372 $tdate["mon"] = substr($sqldate,5,2);
373 $tdate["mday"] = substr($sqldate,8,2);
374 $tdate["hours"] = substr($sqldate,11,2);
375 $tdate["minutes"] = substr($sqldate,14,2);
376 $tdate["seconds"] = substr($sqldate,17,2);
377 $newdate=mktime($tdate["hours"] + $hour, $tdate["minutes"] + $min, $tdate["seconds"] + $sec, $tdate["mon"] + $month, $tdate["mday"] + $day, $tdate["year"] + $year);
378 date_default_timezone_set("UTC");
379 //echo self::toSQLDateTime($date) . " => " . self::toSQLDateTime($newdate) . " ($hour:$min:$sec $month/$day/$year)<br/>\n";
380 return $newdate;
381 }
382
383 /**
384 * Date math: get date from week and day in specifiec month
385 *
386 * This routine finds actual dates for the second Tuesday of the month, last Friday of the month, etc.
387 * For second Tuesday, use $week = 1, $wday = 2
388 * for last Friday, use $week = -1, $wday = 5
389 *
390 * @param int $date Unix timestamp
391 *
392 * @param int $week week number, 0 is first week, -1 is last
393 *
394 * @param int $wday day of week, 0 is Sunday, 6 is Saturday
395 *
396 * @param string $tzid PHP supported timezone
397 *
398 * @return int Unix timestamp
399 */
400 static function getDateFromDay($date, $week, $wday,$tzid="UTC") {
401 //echo "getDateFromDay(" . self::toSqlDateTime($date) . ",$week,$wday)<br/>\n";
402 // determine first day in month
403 $tdate = getdate($date);
404 $monthbegin = gmmktime(0,0,0, $tdate["mon"],1,$tdate["year"]);
405 $monthend = self::addDate($monthbegin, 0,0,0,1,-1,0,$tzid); // add 1 month and subtract 1 day
406 $day = self::addDate($date,0,0,0,0,1 - $tdate["mday"],0,$tzid);
407 $month = array(array());
408 while($day <= $monthend) {
409 $tdate=getdate($day);
410 $month[$tdate["wday"]][]=$day;
411 //echo self::toSQLDateTime($day) . "<br/>\n";
412 $day = self::addDate($day, 0,0,0,0,1,0,$tzid); // add 1 day
413 }
414 $dayinmonth=0;
415 if($week >= 0)
416 $dayinmonth = $month[$wday][$week];
417 else
418 $dayinmonth = $month[$wday][count($month[$wday]) - 1];
419 //echo "return " . self::toSQLDateTime($dayinmonth);
420 //exit;
421 return $dayinmonth;
422 }
423
424 /**
425 * Convert UTC date-time to local date-time
426 *
427 * @param string $sqldate SQL date-time string
428 *
429 * @param string $tzid PHP recognized timezone (default is "UTC")
430 *
431 * @return string SQL date-time string
432 */
433 static function toLocalDateTime($sqldate, $tzid = "UTC" ){
434 try
435 {
436 $timezone = new DateTimeZone($tzid);
437 }
438 catch(Exception $e)
439 {
440 // bad time zone specified
441 return $sqldate;
442 }
443 $udate = self::toUnixDateTime($sqldate);
444 $daydatetime = new DateTime("@" . $udate);
445 $tzoffset = $timezone->getOffset($daydatetime);
446 return self::toSqlDateTime($udate + $tzoffset);
447 }
448
449 /**
450 * Convert local date-time to UTC date-time
451 *
452 * @param string $sqldate SQL date-time string
453 *
454 * @param string $tzid PHP recognized timezone (default is "UTC")
455 *
456 * @return string SQL date-time string
457 */
458 static function toUTCDateTime($sqldate, $tzid = "UTC" ){
459
460 date_default_timezone_set("UTC");
461 try
462 {
463 $date = new DateTime($sqldate, $tzid);
464 }
465 catch(Exception $e)
466 {
467 // bad time zone specified
468 return $sqldate;
469 }
470 $offset = $date->getOffsetFromGMT();
471 if($offset >= 0)
472 $date->sub(new DateInterval("PT".$offset."S"));
473 else
474 $date->add(new DateInterval("PT".abs($offset)."S"));
475 return $date->toSql(true);
476 }
477
478 /**
479 * Convert from a relative date to an absolute date
480 *
481 * Examples of relative dates are "-2y" for 2 years ago, "18m"
482 * for 18 months after today. Relative date uses "y", "m" and "d" for
483 * year, month and day. Relative date can be combined into comma
484 * separated list, i.e., "-1y,-1d" for 1 year and 1 day ago.
485 *
486 * @param string $date relative date string (i.e. "1y" for 1 year from today)
487 *
488 * @param string $rdate reference date, or blank for current date (in SQL date-time format)
489 *
490 * @return string in SQL date-time format
491 */
492 static function getAbsDate($date,$rdate = ""){
493 if(str_replace(array("y","m","d","h","n"),"",strtolower($date)) != strtolower($date)){
494 date_default_timezone_set("UTC");
495 if($rdate == "")
496 $udate = time();
497 else
498 $udate = self::toUnixDateTime($rdate);
499 $values=explode(",",strtolower($date));
500 $y = 0;
501 $m = 0;
502 $d = 0;
503 $h = 0;
504 $n = 0;
505 foreach($values as $value){
506 $rtype = substr($value,strlen($value)-1);
507 $rvalue = intval(substr($value,0,strlen($value) - 1));
508 switch($rtype){
509 case 'y':
510 $y = $rvalue;
511 break;
512 case 'm':
513 $m = $rvalue;
514 break;
515 case 'd':
516 $d = $rvalue;
517 break;
518 case 'h':
519 $h = $rvalue;
520 break;
521 case 'n':
522 $n = $rvalue;
523 break;
524 }
525 // for "-" values, move to start of day , otherwise, move to end of day
526 if($rvalue[0] == '-')
527 $udate = mktime(0,0,0,date('m',$udate),date('d',$udate),date('Y',$udate));
528 else
529 $udate = mktime(0,-1,0,date('m',$udate),date('d',$udate)+1,date('Y',$udate));
530 $udate = self::addDate($udate,$h,$n,0,$m,$d,$y);
531 }
532 $date = self::toSqlDateTime($udate);
533 }
534 return $date;
535 }
536
537 /**
538 * Format Unix timestamp to iCal date-time format
539 *
540 * @param int $datetime Unix timestamp
541 *
542 * @return string iCal date-time string
543 */
544 static function toiCalDateTime($datetime = null){
545 date_default_timezone_set('UTC');
546 if($datetime == null)
547 $datetime = time();
548 return gmdate("Ymd\THis",$datetime);
549 }
550
551 /**
552 * Format Unix timestamp to iCal date format
553 *
554 * @param int $datetime Unix timestamp
555 *
556 * @return string iCal date-time string
557 */
558 static function toiCalDate($datetime = null){
559 date_default_timezone_set('UTC');
560 if($datetime == null)
561 $datetime = time();
562 return gmdate("Ymd",$datetime);
563 }
564 }
565