<?php /* This script generates a LaTeX include file that contains macros specifying the date ranges for every week of the three main teaching periods through the year (i.e., summer school, first semester, second semester). Simply set the configuration as outlined below and run the script, saving the output in a suitable location, ideally only once for all papers. A good spot would be somewhere in the TeX search path. */ // LaTeX macro names can't include numbers, so we have to convert them to words. require 'Numbers/Words.php'; /* Teaching period configuration. It's easily extensible to a new teaching period simply by adding a new specification for that period. The key should be valid as a LaTeX macro name (i.e., no spaces, no digits, no punctuation). The components of each period specification are: first_week: The academic week number of the first week of the period. last_week: The academic week number of the last week of the period. break_weeks: A list of breaks that occur during the teaching period. Each is specified by the start (break_start) and end (break_end) week. If a break is only one week long, then break_start and break_end should be equal. If there are no breaks in the teaching period, include a single break specification with both break_start and break_end set to zero. The list should ideally be in ascending order, but the script doesn't assume this and sorts the list anyway. Weeks are numbered by academic week number (i.e., week 1 is the first academic week of the year). The date of the first Monday of the year is stored in $first_monday below (everything is Monday-indexed). Note that academic week is normally the same as absolute calendar week. Example: $periods = array( 'SummerSchool' => array( 'first_week' => 2, 'last_week' => 7, 'break_weeks' => array( array( 'break_starts' => 0, 'break_ends' => 0, ), ), ), 'SemesterOne' => array( 'first_week' => 9, 'last_week' => 22, 'break_weeks' => array( array( 'break_starts' => 14, 'break_ends' => 14, ), ), ), 'SemesterTwo' => array( 'first_week' => 28, 'last_week' => 41, 'break_weeks' => array( array( 'break_starts' => 35, 'break_ends' => 35, ), ), ), 'FullYear' => array( 'first_week' => 9, 'last_week' => 41, 'break_weeks' => array( array( 'break_starts' => 23, 'break_ends' => 27, ), array( 'break_starts' => 15, 'break_ends' => 15, ), array( 'break_starts' => 35, 'break_ends' => 35, ), ), ), ); */ require 'calendar_config.php'; // Get first Monday of week 1 according to ISO 8601. $year = date("Y"); $first_monday = new DateTime(); $first_monday->setISODate($year, 1); // A couple of handy date intervals: four days to the end of the current week, // and seven days to the start of next week. $plus_four_days = new DateInterval( 'P4D' ); $plus_seven_days = new DateInterval( 'P7D' ); $numwords = new Numbers_Words(); printf( "%% Teaching calendar dates for %s.\n", $year ); foreach ( $periods as $period_name => $period_data ) { $week_start = clone( $first_monday ); // Jump forward the correct number of weeks to the start of the teaching period. $week_start->add( new DateInterval( sprintf( "P%dD", ( $period_data['first_week'] - 1 ) * 7 ) ) ); $week_number = 0; $num_breaks = 0; // Grab the first break week off the front of the list. sort( $period_data['break_weeks'] ); $break_week = array_shift( $period_data['break_weeks'] ); for ( $week = $period_data['first_week']; $week <= $period_data['last_week']; $week++ ) { $week_end = clone( $week_start ); $week_end->add( $plus_four_days ); // Handle break weeks. if ( ( $week >= $break_week['break_starts'] ) && ( $week <= $break_week['break_ends'] ) ) { if ( $week == $break_week['break_starts'] ) { $num_breaks++; // Work out the end date of the break. $break_end = clone( $week_start ); // Note: +4 days to get to Friday. $break_end->add( new DateInterval( sprintf( "P%dD", ( ( $break_week['break_ends'] - $break_week['break_starts'] ) * 7 ) + 4 ) ) ); // Output macro for inclusion in paper calendar. printf( "\\newcommand{\\%sBreak%s}{%s--%s}\n", $period_name, str_replace( "-", "", ucfirst( $numwords->toWords( $num_breaks ) ) ), // If the week start and end dates are in the same month, we only // need to output the month name once. ( $week_start->format( 'n' ) == $break_end->format( 'n' ) ) ? $week_start->format( 'j' ) : $week_start->format( 'j M' ), $break_end->format( 'j M' ) ); } // if current week == first week of break if ( $week == $break_week['break_ends'] ) { // Grab the next break week off the front of the list. $break_week = array_shift( $period_data['break_weeks'] ); } // if current week == last week of break } // if current week is a break week else { $week_number++; // If the week start and end dates are in the same month, we only // need to output the month name once. if ( $week_start->format( 'n' ) == $week_end->format( 'n' ) ) { // Output macro for inclusion in paper calendar. printf( "\\newcommand{\\%sWeek%s}{%s--%s \\\\ %s}\n", $period_name, str_replace( "-", "", ucfirst( $numwords->toWords( $week_number ) ) ), $week_start->format( 'j' ), $week_end->format( 'j' ), $week_end->format( 'M' ) ); } // if same month else { // Output macro for inclusion in paper calendar. printf( "\\newcommand{\\%sWeek%s}{%s \\\\ --%s}\n", $period_name, str_replace( "-", "", ucfirst( $numwords->toWords( $week_number ) ) ), $week_start->format( 'j M' ), $week_end->format( 'j M' ) ); } // else different months } // else normal teaching week $week_start->add( $plus_seven_days ); } // for each week } // foreach teaching period ?>