...

View Full Version : Critique my calendar class



mothra
06-10-2006, 04:20 AM
I wanted to learn something about date functions so I wrote a calendar class. There isn't much too it, if anyone would be as kind as to check it out and give me your thoughts. I don't know if the methods I'm using to get the current date, days in month, first day of the month, etc... are all fairly reliable or not.

Here is the class code:

<?
class Easy_Cal {

var $_timestamp;// timestamp for today
var $_year; // 4-digit year string
var $_month; // month string
var $_today; // day of the month for today
var $_days; // number of days in the month


// defaults day of the week heading strings
var $_sun = "Sun";
var $_mon = "Mon";
var $_tue = "Tue";
var $_wed = "Wed";
var $_thu = "Thu";
var $_fri = "Fri";
var $_sat = "Sat";


function cal_init() {
// initializes the critical parameters of the class
$this->_timestamp = getdate(time());
$this->_year = $this->_timestamp["year"];
$this->_month = $this->_timestamp["month"];
$this->_today = $this->_timestamp["mday"];
$this->_days = cal_days_in_month(CAL_GREGORIAN, $this->_timestamp["mon"], $this->_year);
}


function cal_set_day_text($sun, $mon, $tue, $wed, $thu, $fri, $sat) {
// use this function to change the text for the day of the week headings
$this->_sun = $sun;
$this->_mon = $mon;
$this->_tue = $tue;
$this->_wed = $wed;
$this->_thu = $thu;
$this->_fri = $fri;
$this->_sat = $sat;
}


function cal_get_days() {
// writes the days of the month to the calendar
echo "<tr>";

// $w is the weekday counter
// offset the first day of the month if necessary
$offset = getdate(mktime (0, 0, 0, $this->_timestamp["mon"], 1, $this->_timestamp["year"]));
$offset = $offset["wday"];
for ($w = 1; $w <= $offset; $w++) {
echo "<td></td>";
}

// $d is the day counter
$d = 1;

// this loop to build the calendar rows
for (;$d <= $this->_days;) {
// this loop to build the days in each row
for ($w = $w; $w <= 7; $w++) {
if ($d != $this->_today) {
echo "<td class=\"calDay\"> $d </td>";
} else {
echo "<td class=\"calToday\"> $d </td>";
}
$d++;
if ($d > $this->_days) {break;}
}
$w = 1;

echo "</tr>";
}
}


function cal_fast() {
// canned function to create a simple calendar in one step
$this->cal_init();

echo "<table class=\"calTable\">";
echo "<tr>";
echo "<td colspan=\"7\" class=\"calHeader\">" . $this->_month . " " . $this->_year . "</td>";
echo "</tr>";
echo "<tr>";
echo "<td> $this->_sun </td>";
echo "<td> $this->_mon </td>";
echo "<td> $this->_tue </td>";
echo "<td> $this->_wed </td>";
echo "<td> $this->_thu </td>";
echo "<td> $this->_fri </td>";
echo "<td> $this->_sat </td>";
echo "</tr>";

$this->cal_get_days();

echo "</table>";
}

}
?>


and here are the CSS styles it implements:


.calTable {
border: 1px solid rgb(84,125,177);
border-collapse: collapse;
text-align: center;
}

.calHeader{
background-color: rgb(184,206,230);
}

.calDay {

}

.calToday {
border: 2px solid rgb(84,125,177);
}


Aside from putting the styles into your style sheet (and making any changes to them that you want) this is really all you should need to do to create a calendar :


<?php
include './clsEasyCal.php';

$cal = new Easy_Cal;
$cal->cal_fast();
?>


The class exposes all of its functions and members so I suppose you could piece the calendar together in your html for a little more flexibility, otherwise just use the cal_fast function :)

I plan to add scrolling to the next/previous month. And I would like to add the ability to add events but I'm not sure where to start with that one.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum