Here's the code...
<?php
/*
Calendar v 2.5.1
By: Stan Lemon ([url]www.stanlemon.net[/url])
Original by: Jim Wright
2003.06.30
As I continue to develop my dream "Calendar" class I have made several modifications
to my original alteration of Jim Wright's.
A big part of this version was the removal of several functions, and the removal of
the jdmonthname() and juliantojd() because not everyone has Calendar support enabled. The
functions which I removed are: getWeekOfMonth() isLeapYear() and calendarInterface() because
they were no longer used by the calendar generator.
As I continue to learn more about object oriented programming I will also continue to
clean up this code. I am also working to make it more customizable, and in turn, provide
for a more flexible, and more usable calendar class.
There is also error checking, to make sure that we get valid information.
Here is a simple list of things I have left to do:
- Customize total look of calendar (This will be a 3.0 change)
- Query string preserver (This is REALLY important!)
- Make the rest of these functions more usable outside of generating a calendar.
- Marking of current date.
- Possibly implement some JavaScript to allow for "reminders"
*/
class Calendar {
function getDayOfWeek($dd, $mm, $yy){
//returns int giving day of week for a date
$dateArray = getdate(mktime(0,0,0,$mm,$dd,$yy));
return $dateArray['wday'];
}
function daysInMonth($month,$year) {
$date = getdate(mktime(0,0,0,$month+1,0,$year));
return $date["mday"];
}
function startCalendar($myCal) {
if ((isset($_GET['month']) && isset($_GET['year']))) {
$thecalendar = $this->displayCalendar($_GET['month'],$_GET['year'],$myCal);
}
else {
$now = date('n,Y');
$myDate = explode(",",$now);
$thecalendar = $this->displayCalendar($myDate[0],$myDate[1],$myCal);
}
return $thecalendar;
}
function checkDate($month,$year) {
if (!is_numeric($month) || !is_numeric($year)) {
return $this->myError("Either the month of year was non-numeric.");
}
else if ($month > 12 || $month < 1) {
return $this->myError("The number passed for the month value is not valid.");
}
else if ($year > 2037 || $year < 1970) {
return $this->myError("The number passed for the year value is not valid.");
}
else {
return "passed";
}
}
function next($month,$year) {
if ($month == 12) {
$next['month'] = 1;
$next['year'] = $year + 1;
}
else {
$next['month'] = $month + 1;
$next['year'] = $year;
}
return $next;
}
function last($month,$year) {
if ($month == 1) {
$last['month'] = 12;
$last['year'] = $year - 1;
}
else {
$last['month'] = $month - 1;
$last['year'] = $year;
}
return $last;
}
function monthName($month) {
$monthName = array(1 => "Januar",
2 => "Februar",
3 => "Mars",
4 => "April",
5 => "Mai",
6 => "Juni",
7 => "Juli",
8 => "August",
9 => "September",
10 => "Oktober",
11 => "November",
12 => "Desember");
return $monthName[$month];
}
function displayCalendar($month,$year,$myCal){
// Make sure that we have a valid month and year.
$check = $this->checkDate($month,$year);
if ($check != "passed") {
return $check;
die();
}
if ($myCal) {
extract($myCal);
}
$monthName = $this->monthName($month);
$next = $this->next($month,$year);
$last = $this->last($month,$year);
$myCalendar = '
<table border="1" cellpadding="0" cellspacing="0" style="font-size: 8pt; font-family: tahoma, arial, sans-serif;">
<tr>
<td colspan="7" align="center">
<table bgcolor="' . $headerBgcolor . '" border="0" cellpadding="1" cellspacing="0" width="100%" style="font-family: tahoma, arial, sans-serif; font-size: 8pt;">
<tr>
<td align="center" width="30">
<a href="' . $_SERVER['PHP_SELF'] . '?month=' . $last['month'] . '&year=' . $last['year'] . '" style="text-decoration: none;"><<</a>
</td>
<td align="center" style="font-size: 10pt; color: ' . $headerColor . '">
' . $monthName . ' ' . $year . '
</td>
<td align="center" width="30">
<a href="' . $_SERVER['PHP_SELF'] . '?month=' . $next['month'] . '&year=' . $next['year'] . '" style="text-decoration: none;">>></a>
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td bgcolor="' . $dayBgcolor . '" width="27" align="center"valign="middle" style="color: ' . $dayColor . '">
Søn
</td>
<td bgcolor="' . $dayBgcolor . '" width="27" align="center"valign="middle" style="color: ' . $dayColor . '">
Man
</td>
<td bgcolor="' . $dayBgcolor . '" width="27" align="center"valign="middle" style="color: ' . $dayColor . '">
Tir
</td>
<td bgcolor="' . $dayBgcolor . '" width="27" align="center"valign="middle" style="color: ' . $dayColor . '">
Ons
</td>
<td bgcolor="' . $dayBgcolor . '" width="27" align="center"valign="middle" style="color: ' . $dayColor . '">
Tor
</td>
<td bgcolor="' . $dayBgcolor . '" width="27" align="center"valign="middle" style="color: ' . $dayColor . '">
Fre
</td>
<td bgcolor="' . $dayBgcolor . '" width="27" align="center"valign="middle" style="color: ' . $dayColor . '">
Lør
</td>
</tr>';
$dom = 1;
//scan thru calendar grid
for ($r=1; $r<=6; $r++){
for ($c=0; $c<=7; $c++){
if ($c == 0){
$myCalendar .= "
<tr>";
}
else {
if ($c == 1 + $this->getDayOfWeek($dom,$month,$year)){
$myCalendar .= '
<td align="center" valign="middle" bgcolor="' . $cellBgcolor . '" style="color: ' . $cellColor . '">';
//Her er kode for å sjekke om det finnes noe på denne dagen
$dag = $year . str_pad(($month),2,0,STR_PAD_LEFT) . str_pad(($dom),2,0,STR_PAD_LEFT);
$sql = "SELECT *,DATE_FORMAT(til, '%Y') as tilar,DATE_FORMAT(til, '%m') as tilmnd,DATE_FORMAT(til, '%d') as tildag FROM vakter WHERE lagid = '".$_SESSION['admin_login_lag']."'";
$result = mysql_query($sql) or die(mysql_error());
while ($row = mysql_fetch_array($result)) {
$vaktdag = $row[tilar].str_pad(($row[tilmnd]),2,0,STR_PAD_LEFT).str_pad(($row[tildag]),2,0,STR_PAD_LEFT);
if ($dag == $vaktdag) {
$sjekk = 1;
}
$vla = $vaktdag;
}
if ($sjekk) {
$dagen = "<a href=\"jadja\" style=\"color: red\">".$dom."</a>";
} else {
$dagen = $dom;
}
$myCalendar .= '
' . $dagen . '
</td>';
$sjekk = 0;
//if there are no more days, skip drawing the rest
if ($dom < $this->daysInMonth($month,$year)){
$dom++;
}
else {
$r=7;
}
}
else {
$myCalendar .= '
<td bgcolor="' . $cellBgcolor . '">
</td>';
}
}
if ($c == 7){
$myCalendar .= "
</tr>";
}
}
}
$myCalendar .= "
</table>";
return $myCalendar;
}
function myError($myError) {
$returnError = '<div>
<strong>The following calendar error has occured:</strong><br />
' . $myError . '
</div>';
return $returnError;
}
}
// First configure this bad boy.
$myCal['headerBgcolor'] = "#dddddd";
$myCal['headerColor'] = "#000000";
$myCal['dayBgcolor'] = "#bbbbbb";
$myCal['dayColor'] = "#000000";
$myCal['cellBgcolor'] = "#eeeeee";
$myCal['cellColor'] = "#0063c6";
// Start your engines!
$newCal = new Calendar();
print $newCal->startCalendar($myCal);
?>
</body>
</html>