-
Notifications
You must be signed in to change notification settings - Fork 11
/
calendar.php
89 lines (76 loc) · 2.04 KB
/
calendar.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
<?php
date_default_timezone_set('Europe/London');
function getMiddleDay($day, $month, $year) {
$thisMonth = mktime(0, 0, 0, $month, 1, $year);
$daysInMonth = date('t', $thisMonth);
$middleTimestamp = mktime(0, 0, 0, $month, floor($daysInMonth/2), $year);
$potentials = array();
foreach (array(2,3) as $i) {
$timestamp = strtotime("{$i} {$day}", $thisMonth);
$potentials[abs($timestamp - $middleTimestamp)] = $timestamp;
}
ksort($potentials);
return array_shift($potentials);
}
header('Content-Type: text/calendar');
ob_start();
// Google calendar explodes if you indent ical. Seriously.
?>
BEGIN:VCALENDAR
VERSION:2.0
PRODID:-//PUBSTANDARDS//PUBCAL 1.0//EN
CALSCALE:GREGORIAN
METHOD:PUBLISH
X-WR-CALNAME:Pub Standards
BEGIN:VTIMEZONE
TZID:Europe/London
X-LIC-LOCATION:Europe/London
BEGIN:DAYLIGHT
TZOFFSETFROM:+0000
TZOFFSETTO:+0100
TZNAME:BST
DTSTART:19700329T010000
RRULE:FREQ=YEARLY;BYMONTH=3;BYDAY=-1SU
END:DAYLIGHT
BEGIN:STANDARD
TZOFFSETFROM:+0100
TZOFFSETTO:+0000
TZNAME:GMT
DTSTART:19701025T020000
RRULE:FREQ=YEARLY;BYMONTH=10;BYDAY=-1SU
END:STANDARD
END:VTIMEZONE
<?php
// Strtotime is dumb when the number of days in the current month is
// larger than the next, so drop back to the 28th to avoid this
$currentTime = time();
if (date('j') > 28) {
$currentTime = mktime(date('H'), date('i'), date('s'), date('n'), 28);
}
for ($i = 0; $i < 100; $i++) {
$nextMonth = strtotime("{$i} month", $currentTime);
$timestamp = getMiddleDay(
'thursday',
date('n', $nextMonth),
date('Y', $nextMonth)
);
?>
BEGIN:VEVENT
UID:<?=$timestamp?>PS
DTSTAMP:<?=date('Ymd')?>T<?=date('H')?>0000Z
ORGANIZER:[email protected]
LOCATION:The Bricklayers Arms\, Gresse Street\, London\, W1
DTSTART:<?=date('Ymd', $timestamp)?>T180000
SUMMARY:Pub Standards
DTEND:<?=date('Ymd', $timestamp)?>T233000
DESCRIPTION:Beer\, lots of beer.
END:VEVENT
<?php
}
?>
END:VCALENDAR
<?php
# iCal DEMANDS lines end in CRLF. The twat.
$output = ob_get_contents();
ob_end_clean();
print str_replace("\n", "\r\n", $output);