#native_company# #native_desc#
#native_cta#

phpCalendar2

By Jason Loretz
on December 11, 2000

Version: 1.0

Type: Full Script

Category: Calendars/Dates

License: Other

Description: This is another simple calendar but listed again because now it includes minimal database support (PostgreSQL). Gives month and day views.

*** Save Script As pgsql.sql And Run To Create Database ***

-- create the database for phpCalendar
-- Run using: psql template1 < pgsql.sql
-- Run as: postgres

CREATE DATABASE calendardb;

connect calendardb

CREATE TABLE calendar_dates (
	calid		serial not null primary key,
	date_when	date,
	time_when	time,
	description	text
);

-- Sample INSERT Command
INSERT INTO calendar_dates(date_when,time_when,description)
                    values(date '2000-12-25', time '09:00:00', 'Christmas');

GRANT SELECT, INSERT, UPDATE ON calendar_dates TO phpuser;

---------------------- Divide Scripts Here ----------------------
*** Save Script As Calendar2.php or change link names within script ***

<?
/* Author: Jason C. Loretz
	   [email protected]
   Description: A simple calendar with database support.
   License: NONE, Except that you include this header.
*/

// Database variables (Adjust to match your database)
$default_dbname="calendardb";
$default_host="127.0.0.1";
$default_port="5432";
$default_user="phpuser";
$default_password="phpuser";

// HTML Color Schemes
$header_bgcolor = "#000000";
$header_txtcolor = "#FFFFFF";
$title_bgcolor = "#000066";
$title_txtcolor = "#FFFFFF";
$text_bgcolor = "#FFFFFF";
$text_txtcolor = "#000000";
$date_bgcolor = "#FFFFFF";
$extra_bgcolor = "#CCCCCC";
$weekend_txtcolor = "#666699";
$table_width = 350;
$column_width = 50;

#######################
## Program Functions ##
#######################
// Connect to the PostgreSQL Database
function connectPGSQL($default_dbname,$default_host,$default_port,$default_user,$default_password) {
	$connection = pg_connect("dbname=$default_dbname host=$default_host port=$default_port
				  user=$default_user password=$default_password");
	if (!$connection) {
		echo "ERROR: Couldn't open the database.n";
		exit;
	} else {
		return ($connection);
	}
}

// Disconnect from the PostgreSQL Database
function disconnectPGSQL($connection) {
	$connection = pg_close($connection);
	if (!$connection) {
		echo "ERROR: Couldn't close the database connection.n";
		exit;
	} else {
		return;
	}
}

// Generate the month text for the numbers (returns a string)
function genMonth_Text($month) {
	switch ($month) {
		case 1: $month_text = "January"; break;
		case 2: $month_text = "February"; break;
		case 3: $month_text = "March"; break;
		case 4: $month_text = "April"; break;
		case 5: $month_text = "May"; break;
		case 6: $month_text = "June"; break;
		case 7: $month_text = "July"; break;
		case 8: $month_text = "August"; break;
		case 9: $month_text = "September"; break;
		case 10: $month_text = "October"; break;
		case 11: $month_text = "November"; break;
		case 12: $month_text = "December"; break;
	}
	return ($month_text);
}

// Set Today's Month (returns numerical value)
function genSet_Month() {
	$month = date(m,time());
	return ($month);
}

// Set Today's Year (returns numerical value)
function genSet_Year() {
	$year = date(Y,time());
	return ($year);
}

// Generate the Stop Value (returns the number of days in the month)
function genSet_Stop($month,$year) {
	if ($month == '12') {
		$month = 1;
		$year++;
	} else {
		$month++;
	}
	$stop = mktime(0,0,0,$month,0,$year);
	$stop = date(d,$stop);
	return ($stop);
}

// Generate the top HTML (returns HTML string)
function genTop_HTML() {
	$string = "<html>n<head>nt<title>phpCalendar.php</title>n</head>n" .
		  "<body bgcolor="#FFFFFF" alink="#FF0000" link="#000000" vlink="#000000">n" .
		  "<center>n";

	return ($string);
}

// Generate the end HTML (returns HTML string)
function genEnd_HTML() {
	$string = "</center>n" .
		  "</body>n</html>";

	return ($string);
}

// Generate the table header (returns HTML string)
function genTable_Header($table_width,$header_bgcolor) {
	$string = "<table width="$table_width" cellpadding="1" cellspacing="0">" .
		  "<tr><td bgcolor="$header_bgcolor">n" .
		  "<table width="$table_width" cellpadding="0" cellspacing="1">n";

	return ($string);
}

// Generate the table footer (returns HTML string)
function genTable_Footer() {
	$data = " Created: " . date(M,time()) . " " . date(j,time()) . ", " . date(Y, time());
	$string = "</table></td></tr><tr><td bgcolor="#FFFFFF" align="right">n" .
		  "<font face="Arial" color="#808080" size="1">" .
		  "$data</font></td></tr>n" .
		  "</table>n";

	return ($string);
}

// Generates the information to be displayed
function genCalendar_Month($month,$year,$stop,$connection,
			   $column_width,$extra_bgcolor,$weekend_txtcolor,$date_bgcolor,
			   $header_bgcolor,$header_txtcolor,
			   $title_bgcolor, $title_txtcolor) {
	if ($month == 12) {
		$prev_month = $month - 1;
		$prev_year = $year;
		$next_month = 1;
		$next_year = $year + 1;
	} elseif ($month == 1) {
		$prev_month = 12;
		$prev_year = $year - 1;
		$next_month = $month + 1;
		$next_year = $year;
	} else {
		$prev_month = $month - 1;
		$prev_year = $year;
		$next_month = $month + 1;
		$next_year = $year;
	}
	$start = mktime(0,0,0,$month,1,$year);
	$start = date(w,$start);
	$month_text = genMonth_Text($month);
	$date_string = $month_text . " - " . $year;
	$string = "<tr><td colspan="1" bgcolor="$header_bgcolor" align="right" valign="center">n" .
		  "<form action="Calendar2.php" method="post">n" .
		  "<input type="hidden" name="month" value="$prev_month">n" .
		  "<input type="hidden" name="year" value="$prev_year">n" .
		  "<input type="hidden" name="mode" value="month">n" .
		  "<input type="image" src="left.gif" border="0" width="25" height="25"></form>n" .
		  "</td><td colspan="5" bgcolor="$header_bgcolor" align="center">n" .
		  "<font color="$header_txtcolor" size="3"><b>$date_string</b></font>n" .
		  "</td><td colspan="1" bgcolor="$header_bgcolor" align="left" valign="center">n" .
		  "<form action="Calendar2.php" method="post">n" .
		  "<input type="hidden" name="month" value="$next_month">n" .
		  "<input type="hidden" name="year" value="$next_year">n" .
		  "<input type="hidden" name="mode" value="month">n" .
		  "<input type="image" src="right.gif" border="0" width="25" height="25"></form>n" .
		  "</td></tr>n" .
		  "<tr><td width="$column_width" bgcolor="$title_bgcolor" align="center">" .
		  "<font color="$title_txtcolor">Sun</font></td>n" .
		  "<td width="$column_width" bgcolor="$title_bgcolor" align="center">" .
		  "<font color="$title_txtcolor">Mon</font></td>n" .
		  "<td width="$column_width" bgcolor="$title_bgcolor" align="center">" .
		  "<font color="$title_txtcolor">Tues</font></td>n" .
		  "<td width="$column_width" bgcolor="$title_bgcolor" align="center">" .
		  "<font color="$title_txtcolor">Wed</font></td>n" .
		  "<td width="$column_width" bgcolor="$title_bgcolor" align="center">" .
		  "<font color="$title_txtcolor">Thurs</font></td>n" .
		  "<td width="$column_width" bgcolor="$title_bgcolor" align="center">" .
		  "<font color="$title_txtcolor">Fri</font></td>n" .
		  "<td width="$column_width" bgcolor="$title_bgcolor" align="center">" .
		  "<font color="$title_txtcolor">Sat</font></td></tr>n";

	$string .= "<tr>";
	for ($i = 0; $i < $start; $i++) {
		$string .= "<td bgcolor="$extra_bgcolor" align="center">&nbsp;</td>n";
	}

	for ($i = 1; $i <= $stop; $i++) {
		$day = mktime(0,0,0,$month,$i,$year);
		$frame = date(w,$day);
		if ($i < 10) {
			$number = 0 . $i;
		} else {
			$number = $i;
		}
		$field = $year . "-" . $month . "-" . $number;
		$calendar = pg_exec($connection, "SELECT * FROM calendar_dates WHERE date_when = '$field'");
		$num = pg_numrows($calendar);
		if ($num > 0) {
			$marker = "<font color="#FF0000">*</font>";
		} else {
			$marker = "";
		}
		if ($frame == 6) {
			$string .= "<td bgcolor="$date_bgcolor" align="center">" .
				   "<font color="$weekend_txtcolor">" .
				   "<a style="color: $weekend_txtcolor" href="Calendar2.php?mode=day&mm=$month&dd=$i&yy=$year">" .
				   "<b>$i</b></a>$marker</font></td></tr><tr>n";
		} elseif ($frame == 0) {
			$string .= "<td bgcolor="$date_bgcolor" align="center">" .
				   "<font color="$weekend_color">" .
				   "<a style="color: $weekend_txtcolor" href="Calendar2.php?mode=day&mm=$month&dd=$i&yy=$year">" .
				   "<b>$i</b></a>$marker</td>n";
		} else {
			$string .= "<td bgcolor="$date_bgcolor" align="center">" .
				   "<a href="Calendar2.php?mode=day&mm=$month&dd=$i&yy=$year">" .
				   "<b>$i</b></a>$marker</td>n";
		}
	}
	for ($i = 1; $frame < 6; $frame++) {
		$string .= "<td bgcolor="$extra_bgcolor" align="center">&nbsp;</td>n";
	}
	if ($frame < 6) { $string .= "</tr>n"; }

	return ($string);
}

// Generate the day calendar (returns HTML string)
function genCalendar_Day($mm,$dd,$yy,$connection,
			 $header_bgcolor,$header_txtcolor,
			 $title_bgcolor,$title_txtcolor,
			 $text_bgcolor,$text_txtcolor) {

	$month_text = genMonth_Text($mm);
	$date_string = $month_text . " " . $dd . ", " . $yy;
	$temp = $dd - 1;
	$temp2 = mktime(0,0,0,$mm,$temp,$yy);
	$prev_month = date(m,$temp2);
	$prev_day = date(j,$temp2);
	$prev_year = date(Y,$temp2);
	$temp = $dd + 1;
	$temp2 = mktime(0,0,0,$mm,$temp,$yy);
	$next_month = date(m,$temp2);
	$next_day = date(j,$temp2);
	$next_year = date(Y,$temp2);
	$today = mktime(0,0,0,$mm,$dd,$yy);
	$day_string = date(l,$today) . ", " . date(F,$today) . " " . date(j,$today) . ", " . date(Y,$today);

	$string = "<tr><td colspan="3" bgcolor="$title_bgcolor" align="center">n" .
		  "<font color="$title_txtcolor"><b>$day_string</b></font>n" .
		  "</td></tr>n<tr><td colspan="3" bgcolor="$text_bgcolor" align="left">n";

	$field = $yy . "-" . $mm . "-" . $dd;
	$calendar = pg_exec($connection, "SELECT * FROM calendar_dates WHERE date_when = '$field' ORDER BY time_when");
	$num = pg_numrows($calendar);
	if ($num == 0) {
		$app1 = "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;No Appointments For Today!<br><br>n";
	} else {
		$app1 = "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
		for ($i = 0; $i < $num; $i++) {
			$arr = pg_fetch_row($calendar, $i);
			$app1 .= substr($arr[2],0,5) . " -- " . $arr[3];
			$app1 .= "<br>n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
		}
		$app1 .= "<br>n";
	}

	$string .= "<br>" .
		   "<font face="Arial" size="3">" .
		   "<b>&nbsp;&nbsp;Today's Appointment's:</b></font><br>n" .
		   $app1 .
		   "<br>";

	$string .= "</td></tr>n" .
		   "<tr><td colspan="3" bgcolor="$text_bgcolor" align="center">n" .
		   "<font face="Arial" size="1">n" .
		   "<a href="Calendar2.php?mode=month&month=$mm&year=$yy">(Back To Month View)</a>" .
		   "</font></td></tr>n" .
		   "<tr><td width="$column_width" bgcolor="$header_bgcolor" align="right">" .
		   "<a href="Calendar2.php?mode=day&mm=$prev_month&dd=$prev_day&yy=$prev_year">". 
		   "<img src="left.gif" border="0" width="25" height="25"></a>" .
		   "</td><td bgcolor="$header_bgcolor" align="center"><font color="$header_txtcolor">" .
		   "$date_string</font>" .
		   "</td><td width="$column_width" bgcolor="$header_bgcolor" align="left">" .
		   "<a href="Calendar2.php?mode=day&mm=$next_month&dd=$next_day&yy=$next_year">" .
		   "<img src="right.gif" border="0" width="25" height="25"></a>" .
		   "</td></tr>";

	return ($string);
}

######################
## Script Execution ##
######################
$connection = connectPGSQL($default_dbname,$default_host,$default_port,$default_user,$default_password);
print $top_html = genTop_HTML();
print $top_html = genTable_Header($table_width,$header_bgcolor);
if (!$mode) { $mode = "month"; }
if ($mode == "month") {
	if (!$month) {
		$month = genSet_Month();
		$year = genSet_Year();
	}
	$day_number = genSet_Stop($month,$year);
	print $mid_html = genCalendar_Month($month,$year,$day_number,$connection,
					    $column_width,$extra_bgcolor,
					    $weekend_txtcolor,$date_bgcolor,
					    $header_bgcolor,$header_txtcolor,
					    $title_bgcolor,$title_txtcolor);
} elseif ($mode == "day") {
	print $mid_html = genCalendar_Day($mm,$dd,$yy,$connection,
					  $header_bgcolor,$header_txtcolor,
					  $title_bgcolor,$title_txtcolor,
					  $text_bgcolor,$text_txtcolor);
}
print $end_html = genTable_Footer();
print $end_html = genEnd_HTML();
disconnectPGSQL($connection);
?>