Skip to content

Instantly share code, notes, and snippets.

@chrisblakley
Last active August 29, 2015 14:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chrisblakley/acbc4e986b2c90ac326e to your computer and use it in GitHub Desktop.
Save chrisblakley/acbc4e986b2c90ac326e to your computer and use it in GitHub Desktop.
Visualize the probability of you being sick in Google Calendar by generating this .ics file.
<?php
// http://gearside.com/calendars/sick.php
$debug = 0; //Enable forced debug mode here
if ( $debug == 0 && !array_key_exists('debug', $_GET) ) {
header('Content-type: text/calendar; charset=utf-8');
header('Content-Disposition: attachment; filename=sickness_probability.ics');
}
$total = max(array(1, date('Y')-2012));
//Whenever you're sick, just add the year to the amount array for that week.
$events = array(
array('week' => '01', 'amount' => 0), //Jan 1
array('week' => '02', 'amount' => 0), //Jan 8
array('week' => '03', 'amount' => 0), //Jan 15
array('week' => '04', 'amount' => 0), //Jan 22
array('week' => '05', 'amount' => 0), //Jan 29
array('week' => '06', 'amount' => 0), //Feb 5
array('week' => '07', 'amount' => 0), //Feb 12
array('week' => '08', 'amount' => array('2013')), //Feb 19
array('week' => '09', 'amount' => 0), //Feb 26
array('week' => '10', 'amount' => 0), //Mar 5
array('week' => '11', 'amount' => 0), //Mar 12
array('week' => '12', 'amount' => 0), //Mar 19
array('week' => '13', 'amount' => array('2015')), //Mar 26
array('week' => '14', 'amount' => 0), //Apr 2
array('week' => '15', 'amount' => 0), //Apr 9
array('week' => '16', 'amount' => 0), //Apr 16
array('week' => '17', 'amount' => 0), //Apr 23
array('week' => '18', 'amount' => 0), //Apr 30
array('week' => '19', 'amount' => 0), //May 7
array('week' => '20', 'amount' => 0), //May 14
array('week' => '21', 'amount' => 0), //May 21
array('week' => '22', 'amount' => 0), //May 28
array('week' => '23', 'amount' => 0), //Jun 4
array('week' => '24', 'amount' => 0), //Jun 11
array('week' => '25', 'amount' => array('2014')), //Jun 18
array('week' => '26', 'amount' => 0), //Jun 25
array('week' => '27', 'amount' => 0), //Jul 2
array('week' => '28', 'amount' => 0), //Jul 9
array('week' => '29', 'amount' => 0), //Jul 16
array('week' => '30', 'amount' => 0), //Jul 23
array('week' => '31', 'amount' => 0), //Jul 30
array('week' => '32', 'amount' => 0), //Aug 6
array('week' => '33', 'amount' => 0), //Aug 13
array('week' => '34', 'amount' => 0), //Aug 20
array('week' => '35', 'amount' => 0), //Aug 27
array('week' => '36', 'amount' => 0), //Sep 3
array('week' => '37', 'amount' => 0), //Sep 10
array('week' => '38', 'amount' => 0), //Sep 17
array('week' => '39', 'amount' => 0), //Sep 24
array('week' => '40', 'amount' => array('2013')), //Oct 1
array('week' => '41', 'amount' => 0), //Oct 8
array('week' => '42', 'amount' => 0), //Oct 15
array('week' => '43', 'amount' => 0), //Oct 22
array('week' => '44', 'amount' => 0), //Oct 29
array('week' => '45', 'amount' => 0), //Nov 5
array('week' => '46', 'amount' => 0), //Nov 12
array('week' => '47', 'amount' => 0), //Nov 19
array('week' => '48', 'amount' => 0), //Nov 26
array('week' => '49', 'amount' => 0), //Dec 3
array('week' => '50', 'amount' => array('2013', '2014')), //Dec 10
array('week' => '51', 'amount' => 0), //Dec 17
array('week' => '52', 'amount' => 0), //Dec 24
);
function dateToCal($timestamp) {
return date('Ymd\THis\Z', $timestamp);
}
function escapeString($string) {
return preg_replace('/([\,;])/','\\\$1', $string);
}
?>
BEGIN:VCALENDAR<?php echo "\r\n"; ?>
VERSION:2.0<?php echo "\r\n"; ?>
PRODID:-//hacksw/handcal//NONSGML v1.0//EN<?php echo "\r\n"; ?>
CALSCALE:GREGORIAN<?php echo "\r\n"; ?>
METHOD:PUBLISH<?php echo "\r\n"; ?>
X-WR-CALNAME:Gearside - Sickness Probability<?php echo "\r\n"; ?>
<?php $i = 0; ?>
<?php foreach ( $events as $event ) : ?>
<?php
if ( $event['amount'] == 0 ) {
continue;
}
$amount = count($event['amount']);
$probability = round($amount/$total*100);
$last = end($event['amount']);
$summary = 'Sick Probability: ' . $probability . '% (Last in ' . $last . ')';
if ( $debug == 1 || array_key_exists('debug', $_GET) ) {
echo "\r\n------------------\r\n";
echo "Last Modified: " . date('l, F j, Y', filemtime(__FILE__)) . "\r\n";
echo "Debug Info\r\n";
echo "Week: " . $event['week'] . "\r\n";
echo "Times Sick: " . $event['amount'] . "\r\n";
echo "Total Years: " . $total . "\r\n";
echo $summary . "\r\n";
echo "Start Date: " . date('F j, Y', strtotime(date('Y') . 'W' . $event['week'])) . "\r\n";
echo "End Date: " . date('F j, Y', strtotime(date('Y') . 'W' . $event['week'] . '+7 days')) . "\r\n";
echo "\r\n";
}
?>
BEGIN:VEVENT<?php echo "\r\n"; ?>
CREATED:<?php echo dateToCal(strtotime($date)) . "\r\n"; ?>
DTSTART;VALUE=DATE:<?php echo date('Ymd', strtotime(date('Y') . 'W' . $event['week'])) . "\r\n"; ?>
DTEND;VALUE=DATE:<?php echo date('Ymd', strtotime(date('Y') . 'W' . $event['week'] . '+7 days')) . "\r\n"; ?>
DTSTAMP:<?php echo dateToCal(strtotime(date('Y') . 'W' . $event['week'])) . "\r\n"; ?>
LAST-MODIFIED:<?php echo dateToCal(filemtime(__FILE__)) . "\r\n"; ?>
UID:<?php echo md5(uniqid(mt_rand(), true)) . "@gearside.com" . "\r\n"; ?>
DESCRIPTION:<?php echo escapeString($summary) . "\r\n"; ?>
URL;VALUE=URI:<?php echo escapeString('http://gearside.com/calendars/sick.ics') . "\r\n"; ?>
SUMMARY:<?php echo escapeString($summary) . "\r\n"; ?>
<?php //X-GOOGLE-CALENDAR-CONTENT-ICON:https://www.google.com/calendar/images/daynum/99_v2.png . "\r\n"; ?>
RRULE:FREQ=YEARLY<?php echo "\r\n"; ?>
END:VEVENT<?php echo "\r\n"; ?>
<?php $i++; ?>
<?php endforeach; ?>
END:VCALENDAR<?php echo "\r\n"; ?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment