samScripts.com
[Home] [My Scripts] [Contact] [Business Card Designer] [Donate] [$8.95 Domain Names]

Home > Scripts > calendar

calendar

... feel free to use this script for whatever you like.

calendar

Very simple to use calendar class.

Description:

This class provides an easy way to display a calendar on your website. The main features are:

  1. Fully customisable output - easily set the size, colours, etc using html

  2. Choose the day of the week to start the calendar on

  3. Optionally provide an array of "date"=>"event" to display events for each day

  4. Have different colours for days depending on whether they are in the month, the current day, have events on that day, etc.

  5. Support functions for getting the next / previous month / day / week /etc.



Usage example:


As you see, most of it is plain html or setting up the way to display the calendar.

<?php

include("./calendar.php");

if( !isset(
$thedate) )$thedate = date("Y-m-d");

$cal = new calendar($thedate);

?>
<html>
<body>
<head>

<!-- styles for formatted calendar display -->

<style type="text/css">
<!--
.tdinmonth, .tdoutmonth, .thcal, .tdinmonthevent, .tdoutmonthevent, .tdtoday, .tdtodayevent{
font-family: verdana; font-size: 10pt;border-color: #000033; border-style: solid; border-width: 1px;}
.caltable {border-color: black; border-style: solid; border-width: 2px;}
.tdinmonth {background-color: #ffffdd; width: 64px; height: 64px;}
.tdoutmonth {background-color: #eeeeee; width: 64px; height: 64px;}
.thcal {background-color: #aaaaff; font-size: 10px}
.tdinmonthevent{background-color: #ffffaa; width: 64px; height: 64px;}
.tdoutmonthevent{background-color: #cccccc; width: 64px; height: 64px;}
.tdtoday{background-color: #ffdddd; width: 64px; height: 64px;}
.tdtodayevent{background-color: #ffaaaa; width: 64px; height: 64px;}
.navlink {font-family: verdana; font-size: 10px;}
.heading {font-family: verdana; font-size: 12px;}
-->
</style>
</head>
<table width='100%' border=0>
<tr><td align=left valign=top>
<?php

// show a small calendar with no default formatting

$cal->dayformat = "short";
$cal->displaycalendar();

?>
</td><td align=left valign=top>
<table border=0>
<tr><td align=right width='33%'>
<a href='calexample.php?thedate=<?php echo $cal->prevmonth();?>' class='navlink'><?php echo $cal->prevmonth("%B %Y");?></a>
</td>
<td width='33%' align=center>
<span class='heading'><?php echo $cal->getdate("%A %B %d %Y");?></span>
</td>
<td width='33%' align=left>
<a href='calexample.php?thedate=<?php echo $cal->nextmonth();?>' class='navlink'><?php echo $cal->nextmonth("%B %Y");?></a>
</td></tr>
<tr><td align=center colspan=3>
<?php

// set up some imaginary events

$events = array("2002-04-26"=> "An event", "2002-04-02" => "Something!", "2002-05-03" => "Here!");

// set up the calendar layout options

$cal->tabletags = "<table class='caltable'>";
$cal->tdinmonth = "<td class='tdinmonth' align=left valign=top><a href='calexample.php?thedate=%date%'>%day%</a></td>\n";
$cal->tdoutmonth = "<td class='tdoutmonth' align=left valign=top><a href='calexample.php?thedate=%date%'>%day%</a></td>\n";
$cal->tdinmonthevent = "<td align=left valign=top class='tdinmonthevent'><a href='calexample.php?thedate=%date%'>%day%</a><br>%event%</td>\n";
$cal->tdoutmonthevent = "<td align=left valign=top class='tdoutmonthevent'><a href='calexample.php?thedate=%date%'>%day%</a><br>%event%</td>\n";
$cal->dayformat = "full";
$cal->thtags = "<th class='thcal'>%day%</th>";
$cal->tdtoday = "<td align=left valign=top class='tdtoday'><a href='calexample.php?thedate=%date%'>%day%</a></td>\n";
$cal->tdtodayevent = "<td align=left valign=top class='tdtodayevent'><a href='calexample.php?thedate=%date%'>%day%</a><br>%event%</td>\n";

// display the calendar with the events

$cal->displaycalendar($events);

?>
</td></tr>
</table>
</td>
</tr>
</table>
</body>
</html>  


The following variables set how the calendar will be displayed:


  • $calendar->startday; // day of week to start the calendar from - 0=sunday

  • $calendar->tabletags; // html for the table tag enclosing the calendar

  • $calendar->tdinmonth; // when date is in the current month

  • $calendar->tdoutmonth; // when date is outside current month

  • $calendar->tdtoday; // when date is current date

  • $calendar->tdtodayevent; // when there is an event that day

  • $calendar->tdevent; // event but not today

  • $calendar->tdoutevent; // event but out of month

  • $calendar->dayformat; // short, medium or long - ie 'M', 'Mon', 'Monday' for header of the calendar

  • $calendar->thtags; // tags used to display day names



The following functions are available:


  • $calendar->calendar($thedate, $startday);
    constructor - defaults date to today, and startday to sunday

  • $calendar->displaycalendar($events = array());
    Displays the calendar. Events is an optional array of date=>eventttext. If set, then any events in it will be displayed on the corresponding days on the calendar. Example:
    $events = array("2002-04-23"=>"This is an event", "2002-04-25" => "Another event!");
    $calendar->displaycalendar($events);

  • $calendar->setdate($thedate);
    Sets the date that the calendar uses as its current date.

  • $calendar->notaweekend($frimon = -1);
    If the current date falls on a weekend, sets it to either the previous friday, or the following monday, depending on $frimon. (-1 = friday, 1 = monday)

  • The following functions all return some kind of formatted date, relative to whatever date is set as the calendar's current date. They use the same formatting options as the php strftime() function.

    • $calendar->dateadd($format = "%Y-%m-%d", $days = 0, $months = 0, $years = 0, $keepday = true);
      Returns a formatted date which is the current calendar date plus $days days, $months months and $years years.

    • $calendar->getdate($format = "%Y-%m-%d");
      Returns the current calendar date formatted as $format.

    • nextmonth($format = "%Y-%m-%d");

    • prevmonth($format = "%Y-%m-%d");

    • nextweek($format = "%Y-%m-%d");

    • prevweek($format = "%Y-%m-%d");

    • nextday($format = "%Y-%m-%d");

    • prevday($format = "%Y-%m-%d");

    • addweekdays($format = "%Y-%m-%d", $days);
      This returns a new date with the specified number of weekdays added to or subtracted from the current date




Files

calendar.php (7039 bytes) View Download
Viewed 1044 times. Downloaded 1029 times.

[Back] [Top] [Forward]

Powered by php, mySql.

Request processed in 0.00 seconds on Wednesday 26th September 2012 23:43:34.

©2002 Sam Yapp

What do you think of my resume wizard site? All nicely written in php :)