Zend - The PHP Company




Calendars

Add Code


Simple Calendar Class  

Type: class
Added by: clescuyer
Entered: 18/11/2001
Last modified: 31/10/2003
Rating: - (fewer than 3 votes)
Views: 7200
This simple calendar class will display one month with our without day links. Display is controlled by a style sheet, current day, links, daynames are customisable.


<?php

########################################
# CLASSE CALENDAR
#
# Written by :    Christian Lescuyer
# Company :    Go�lette
# URL :        www.goelette.net
# Requires :    PHP 4.0

# History
# V1.0    18/02/2001    Creation
# V1.1    18/11/2001    English comments
#
# Don't forget to update get_id() function with the version number
#
########################################

class calendar {

# Variables
  
var $date;
  var 
$callback;
  var 
$day_name;
  var 
$date_format;

# Constructor
# Set defaults
  
function calendar()
  {
# Use strtotime() format.
    
$this->date date("j M Y");
# Display format
    
$this->date_format="j/m";
    
$this->day_name = array ("Mon""Tue""Wed""Thu""Fri""Sat""Sun");
  }


########################################
# Fonctions set

  
function set_date($text_date) {
    
$this->date $text_date;
  }

  function 
set_date_format($f) {
    
$this->date_format $f;
  }

  function 
set_daynames($names) {
    
$this->day_name $names;
  }

  function 
set_callback($cb) {
    
$this->callback $cb;
  }

########################################
# Fonctions get

  
function get_id() {
    return 
"CLASS calendar v1.1";
  }

  function 
get_date() {
    return(
$this->date);
  }

  function 
get_date_format() {
    return(
$this->date_format);
  }

  function 
get_daynames() {
    return(
$this->day_name);
  }

  function 
get_callback() {
    return(
$this->callback);
  }


########################################
# Main function

  
function output_calendar()
  {
# Relay variable for callback()
# Apparently, it's not possible to dereference $this->callback()
   
$cb $this->callback;

  
# Preliminary calculations
    
$t getdate(strtotime($this->date));
    
$today $t["mday"];
    
$year $t["year"];
    
$month $t["mon"];
  
# Get first day of the month (monday = 0)
    
$first_wday = ((int) date("w"mktime(000$month1$year))+6)%7;
  
# Last day of the month
    
$last_mday = (int) date("d"mktime(000$month+10$year));
    
  
# Anchor
    
echo "<a name="calendar">";
    
  
# Table
    
echo "<TABLE BORDER="0" CELLPADDING="0" CELLSPACING="2">n";
    
  
# Table head
    
echo "<THEAD><TR>";
    for (
$j=$j<=$j++) {
      echo 
"<TH CLASS="calendar_title">n";
      echo 
$this->day_name[$j];
      echo 
"</TH>n";
    }
    echo 
"</TR></THEAD><TBODY>n";
    
  
# Day row
  # A month is displayed on 5 rows
  # except for a 28 days month starting on Monday
    
if (($last_mday==28) and ($first_wday==0))
      
$jmax 27;
    else
      
$jmax 34;
  
    for (
$j=0$j<=$jmax$j++) {
  
# Start new row on Monday
      
if ($j%7==0)
        echo 
"<TR>n";
  
  
# Title colour for current day
      
if ($j == $today+$first_wday-1)
        echo 
"<TD CLASS="calendar_title">n";
      else
        echo 
"<TD CLASS="calendar_day">n";
  
      if ((
$j<$first_wday) or ($j>=$last_mday $first_wday)) {
  
  
# Empty boxes
        
echo "&nbsp;";
      } else {
        if (isset(
$cb))
          echo 
$cb(mktime(000$month$j $first_wday 1$year));
        echo 
date($this->date_formatmktime(000$month$j $first_wday 1$year));
        if (isset(
$cb))
          echo 
"</A>n";
      }
      echo 
"</TD>n";
  
  
# End of row on Sunday
      
if ($j%7==6)
        echo 
"</TR>n";
    }
    echo 
"</TBODY></TABLE>n";
  }
# end class calendar
########################################

########################################
# How to use the class

# Nota : Style sheet
#        calendar_day for days
#        calendar_title for titles
#        calendar_table for the table
#
########################################

echo "<HEAD>
 <STYLE type="
text/css">
   TH.calendar_title {font-family: verdana, arial; font-weight: bold; color: #003333; background-color: #009999; text-align: center; height: 25; width: 40}
   TD.calendar_title {font-family: "
Times new roman"; font-weight: bold; color: #000033; background-color: #009999; text-align: center; height: 25; width: 40}
   TD.calendar_day {font-family: "
Times new roman"; color: #000033; background-color: #0099CC; text-align: center; height: 25; width: 40}
 </STYLE>
</HEAD>"
;

# Callback function if you need a link on dates
# The callback function is called for each day
# Adjust link depending on timestamp ($stamp)
# Returns opening <A> tag

function datelink($stamp)
{
  
$link "<A HREF="" . date("Ymd", $stamp) . ".htm">";
  return(
$link);
}

# Display calendar with your own parameters
$cal = new calendar;

# Calendar will display current month, or choose date with set_date
$cal->set_date("25 dec 2001");
$cal->set_callback("datelink");
# Change day language or names
$cal->set_daynames(array ("Lun""Mar""Mer""Jeu""Ven""Sam""Dim"));

$cal->output_calendar();

echo 
"<H1>Example for " $cal->get_id() . "</H1>";
echo 
"<P>Computed for " $cal->get_date() . "<br>";
echo 
"  with links provided by " $cal->get_callback() . "()<br>";
echo 
"And these day titles : ";
print_r$cal->get_daynames());

?>


Usage Example


See code


Rate This Script





Search



This Category All Categories