/** * CalendarWeek() * * @param $week = Week number, excluding trailing zero. i.e. 53 = last week of the year. Can be passed with $_GET. * @param $year = Year number in the format YYYY. Can be passed with $_GET. * @author David Walsh (http://davidwalsh.name) - credit for original method code. (2009) * @author George Botley <*****@*****.**> - modified for torindul-calendar module, added code comments & updated to PHP 5 (2015). * * @usage In your template use $CalendarWeek(53, 12, 2015) to output a Calendar view, where this example would show the last week in December of 2015. */ function CalendarWeek($week = null, $year = null) { /* If $week is null check to see if $_GET['week'] exists, if not define it as todays week. */ if ($week == null) { $week = isset($_GET['week']) ? $_GET['week'] : date('W'); } /* If $year is null check to see if $_GET['year'] exists, if not define it as todays year. */ if ($year == null) { $year = isset($_GET['year']) ? $_GET['year'] : date('Y'); } /* If $week is less than 1 or greater than 53, default to todays date */ if ($week < '1' || $week > '53') { $week = date('W'); $year = date('Y'); } /* NOTE: Whilst it is not good practice, and I may change this in a later version, we use tabular output and store this within a php variable */ /* Form the start of the Calendar in $calendar */ $calendar = '<table cellpadding="0" cellspacing="0" class="calendar">'; /* Create an array to hold the Day Headings for the Calendar */ $days = array('Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'); /* Get the first day of the given week to use in $week_start_fulldate mktime(). */ /* Create a new instance of DateTime() within PHP. */ $week_start_day = new DateTime(); /* Set the ISO date using the $year and $week provided. */ $week_start_day->setISODate($year, $week); /* Lets get the day from DateTime() */ $week_start_day = $week_start_day->format('d'); /* * As our calendar runs from Sunday to Monday, and DateTime() outputs from Monday, lets minus one. * To do this we will need to convert the String outputted from DateTime() to an integer, at base 10. Then minus one. */ $week_start_day = intval($week_start_day, 10) - 1; /* Get the month number from the given week number and year to use in $week_start_fulldate mktime() */ $week_start_month = date('m', strtotime($year . '-W' . $week)); /* Get the full date for the first day in this week */ $week_start_fulldate = date('Y-m-d', mktime('1', '0', '0', $week_start_month, $week_start_day, $year)); /* Start the Calendar Row to hold Day Names */ $calendar .= '<tr class="calendar-row">'; /* Start Table Data to hold day of the week */ $calendar .= '<td class="calendar-day-head">'; /* Loop through days of the week and display day of the week before closing and opening a new <td> */ $calendar .= implode('<td class="calendar-day-head">', $days); /* Once we reach the end of the week day array, close off the last <td> and end the row */ $calendar .= '</td></tr>'; /* * Lets create some variables to store calendar data * * See php.net/date - Returns a string formatted according to the given format string using the given integer timestamp * See php.net/mktime - Returns the Unix timestamp corresponding to the arguments given. * * $days_in_week = The amount of days in a week. * $days_counter = A variable that stores the current iterative day and is checked against $days_in_month. */ $days_in_week = 7; $day_counter = 0; /* Create a row for the week */ $calendar .= '<tr class="calendar-row">'; /* Start a counter ($list_day). Whilst the counter is less than $days_in_week output a day */ for ($list_day = 1; $list_day <= $days_in_week; $list_day++, $week_start_day++) { /* Override the $week_start_day at this point to get the day from $week_start_fulldate */ $week_start_day = date('j', strtotime($week_start_fulldate)); /* Output opening <td> for the current calendar day */ $calendar .= '<td class="calendar-day">'; /* Output the opening for the day container */ $calendar .= '<div class="day-container">'; /* Output the day number */ $calendar .= '<div class="day-number">' . $week_start_day . '</div>'; /* Fetch events for todays date */ $full_date = $week_start_fulldate; $sqlQuery = new sqlQuery(); $sqlQuery->setFrom('CalendarEvent'); $sqlQuery->addWhere("('{$full_date}'>=StartDate) AND ('{$full_date}'<=EndDate)"); $sqlQuery->setOrderBy('Title ASC'); $result = $sqlQuery->execute(); /* Loop through result and output calendar item */ foreach ($result as $row) { $calendar .= '<p>'; $calendar .= '<a href="' . $this->URLSegment . '/view/' . $this->stringToSEOURL($row['Title']) . '-ID-' . $row['ID'] . '" '; $calendar .= 'title="Open ' . $row["Title"] . ' Event">'; $calendar .= $row['Title']; $calendar .= '</a>'; $calendar .= '</p>'; } /* Output the closing tag for the day container */ $calendar .= '</div>'; /* Output the closing <td> for the current calendar day */ $calendar .= '</td>'; /* Increment day by one */ $week_start_fulldate = date('Y-m-d', strtotime($week_start_fulldate . "+1 days")); } /* Output final row */ $calendar .= '</tr>'; /* Output the end of the table */ $calendar .= '</table>'; /* As we havent printed anything on screen yet, return $calendar for use in template with $Calendar */ return $calendar; }