<?php
/* generate iCal week numbers file (Europe, week starts on monday) */

$year = date('Y');
$years_to_generate = 3;

header('Content-Type: text/plain');
header('Content-Disposition: inline; filename="week_numbers_' . $year . '.ics"');

echo <<<ENDOFHEADER
BEGIN:VCALENDAR
VERSION:2.0
X-WR-CALNAME:Weeks $year
X-WR-TIMEZONE:Europe/Zurich
CALSCALE:GREGORIAN

ENDOFHEADER;

$count = 0;
$loops = ($years_to_generate * 52) + 1;
$date = strtotime($year . '-01-01');

// find monday (w = 1) in the current week
$date_offset = (date('w', $date) - 1) * 24 * 60 * 60;
$date -= $date_offset;


while ($count < $loops) {
	$week_number = date('W',   $date);
	$start_date  = date('Ymd', $date);
	$end_date    = date('Ymd', $date + 7 * 24 * 60 * 60);
	
	echo <<<ENDOFEVENT
BEGIN:VEVENT
DTSTART;VALUE=DATE:$start_date
DTEND;VALUE=DATE:$end_date
SUMMARY:Week $week_number
END:VEVENT

ENDOFEVENT;

	$date += 7 * 24 * 60 * 60; // add one week
	$count++;
}

echo <<<ENDOFFOOTER
END:VCALENDAR

ENDOFFOOTER;
?>
