Location: PHPKode > projects > Open Chamber of Commerce > ococ/admin/date_picker.php
<html>
<head>
<title>Date</title>
<!--
     This is a just carrier for invoking and
     styling the calendar_class_pu.php script.
-->

<style type="text/css">
body {
  background: #ffe;
  font: .9em/1.0 helvetica, arial, sans-serif;
  margin: 0;
  }
.hd {
	position:relative;
	top: 0;
	font-weight: 700;
	text-align: center;
	background: #776;
	color: #fff;
	padding: 4px;
	margin: 0;
	}
#calendarBox {
  border: 0px solid #ddc;
  }
.calendarToday {
  color: #fff !important;
  background: #776;
  }
.calendarHeader {
  font-size: .9em;
  font-weight: 700;
  color: #776;
  }
.calendarDay  {
  margin: 0;
  }
.calendarToday a, .calendarDay a {
  font-size: 1em;
  color: #776;
  text-decoration: underline !important;
  }
.calendarToday a {
  color: #fff;
  }

</style>
</head>

<body>
<p class="hd">Pick-A-Date</p>
<br />
<?php
require '../inc/db.inc';
require '../inc/error.inc';
require '../inc/site_globals.inc';
?>
<?php

// PHP Calendar Class Version 1.4 (5th March 2001)
//  
// Copyright David Wilkinson 2000 - 2001. All Rights reserved.

// Modified for OCOC MAR, 2005 - Rik Nilsson
// -- added getDateLink() method
// -- added MyCalendar class extension
// -- added modified instantiator
// -- added css styling for non-today date

// Modified for OCOC FEB, 2006 - Rik Nilsson
// -- added usage of global month names to accomodate translations
//
// This software may be used, modified and distributed freely
// providing this copyright notice remains intact at the head 
// of the file.
//
// This software is freeware. The author accepts no liability for
// any loss or damages whatsoever incurred directly or indirectly 
// from the use of this script. The author of this software makes 
// no claims as to its fitness for any purpose whatsoever. If you 
// wish to use this software you should first satisfy yourself that 
// it meets your requirements.
//
// URL:   http://www.cascade.org.uk/software/php/calendar/
// Email: hide@address.com
//
// CSS styling of the calendar:
// Three classes are written into the algorithm that prints the cal:
// Style them as follows (example):
// 
// <style type="text/css">
// .calendar {   /* main table style containing the calendar */
//   background: #fff;  /* white background */
//   }
// .calendarHeader {    /* month and day name styles */
//   font-family: helvetica,arial,sans-serif;
//   color: #090; /* dark green */
//   }
// .calendarDay {       /* not-today day style - RHN050528 */
//   margin: 0;
//   }
// .calendarToday {     /* today's date style */
//   color: #f00;       /* bright red */
//   font-weight: 700;  /* bold */
//   }
// </style>
//

class Calendar
{
    /*
        Constructor for the Calendar class
    */
    function Calendar()
    {
    }
    
    /*
        Get the array of strings used to label the days of the week. This array contains seven 
        elements, one for each day of the week. The first entry in this array represents Sunday. 
    */
    function getDayNames()
    {
        return $this->dayNames;
    }
    

    /*
        Set the array of strings used to label the days of the week. This array must contain seven 
        elements, one for each day of the week. The first entry in this array represents Sunday. 
    */
    function setDayNames($names)
    {
        $this->dayNames = $names;
    }
    
    /*
        Get the array of strings used to label the months of the year. This array contains twelve 
        elements, one for each month of the year. The first entry in this array represents January. 
    */
    function getMonthNames()
    {
        return $this->monthNames;
    }
    
    /*
        Set the array of strings used to label the months of the year. This array must contain twelve 
        elements, one for each month of the year. The first entry in this array represents January. 
    */
        var $monthNames;

    function setMonthNames($names)
    {
        $this->monthNames = $GLOBALS['MONTH_NAMES'];
    }
    
    
    
    /* 
        Gets the start day of the week. This is the day that appears in the first column
        of the calendar. Sunday = 0.
    */
      function getStartDay()
    {
        return $this->startDay;
    }
    
    /* 
        Sets the start day of the week. This is the day that appears in the first column
        of the calendar. Sunday = 0.
    */
    function setStartDay($day)
    {
        $this->startDay = $day;
    }
    
    
    /* 
        Gets the start month of the year. This is the month that appears first in the year
        view. January = 1.
    */
    function getStartMonth()
    {
        return $this->startMonth;
    }
    
    /* 
        Sets the start month of the year. This is the month that appears first in the year
        view. January = 1.
    */
    function setStartMonth($month)
    {
        $this->startMonth = $month;
    }
    
    
    
    /*
        Return the URL to link to  for a given date.
        You must override this method if you want to activate the date linking
        feature of the calendar.
        
        Note: If you return an empty string from this function, no navigation link will
        be displayed. This is the default behaviour.
    */
    function getDateLink($day, $month, $year, $tgt)
    {
      if(strlen($day) < 2)
        $day = "0".$day;
      // convert date to db format
      $thisdaysdate = date("Y-m-d", mktime(0,0,0,$month,$day,$year));
      //$link = "javascript:window.opener.document.".$tgt.".value='".date("Y-m-$day", time())."';window.close();";
      $link = "javascript:window.opener.document.".$tgt.".value='".$thisdaysdate."'; window.close();";
      return $link;
    } // end getDateLink


    /*
        Return the HTML for the current month
    */
    function getCurrentMonthView()
    {
        $d = getdate(time());
        return $this->getMonthView($d["mon"], $d["year"]);
    }
    

    /*
        Return the HTML for the current year
    */
    function getCurrentYearView()
    {
        $d = getdate(time());
        return $this->getYearView($d["year"]);
    }
    
    
    /*
        Return the HTML for a specified month
    */
    function getMonthView($month, $year, $tgt)
    {
        return $this->getMonthHTML($month, $year, $tgt);
    }
    

    /*
        Return the HTML for a specified year
    */
    function getYearView($year)
    {
        return $this->getYearHTML($year);
    }
    
    
    
    /********************************************************************************
    
        The rest are private methods. No user-servicable parts inside.
        
        You shouldn't need to call any of these functions directly.
        
    *********************************************************************************/


    /*
        Calculate the number of days in a month, taking into account leap years.
    */
    function getDaysInMonth($month, $year)
    {
        if ($month < 1 || $month > 12)
        {
            return 0;
        }
   
        $d = $this->daysInMonth[$month - 1];
   
        if ($month == 2)
        {
            // Check for leap year
            // Forget the 4000 rule, I doubt I'll be around then...
        
            if ($year%4 == 0)
            {
                if ($year%100 == 0)
                {
                    if ($year%400 == 0)
                    {
                        $d = 29;
                    }
                }
                else
                {
                    $d = 29;
                }
            }
        }
    
        return $d;
    }


    /*
        Generate the HTML for a given month
    */
    function getMonthHTML($m, $y, $tgt, $showYear = 1)
    {
        global $MONTH_NAMES;
    	  global $idate;
        global $tgt;
        $s = "";
        
        $a = $this->adjustDate($m, $y);
        $month = $a[0];
        $year = $a[1];        
        
    	$daysInMonth = $this->getDaysInMonth($month, $year);
    	$date = getdate(mktime(12, 0, 0, $month, 1, $year));
    	
    	$first = $date["wday"];
    	//$monthName = $this->monthNames[$month - 1];
    	$monthName = $MONTH_NAMES[$month - 1];
    	
    	$prev = $this->adjustDate($month - 1, $year);
    	$next = $this->adjustDate($month + 1, $year);
    	
    	if ($showYear == 1)
    	{
    	    $prevMonth = $this->getCalendarLink($prev[0], $prev[1], $tgt);
    	    $nextMonth = $this->getCalendarLink($next[0], $next[1], $tgt);
    	}
    	else
    	{
    	    $prevMonth = "";
    	    $nextMonth = "";
    	}
    	
    	$header = $monthName . (($showYear > 0) ? " " . $year : "");
    	
    	$s .= "<table class=\"calendar\">\n";
    	$s .= "<tr>\n";
    	$s .= "<td align=\"center\" valign=\"top\">" . (($prevMonth == "") ? "&nbsp;" : "<a href=\"$prevMonth\">&lt;&lt;</a>")  . "</td>\n";
    	$s .= "<td align=\"center\" valign=\"top\" class=\"calendarHeader\" colspan=\"5\">$header</td>\n"; 
    	$s .= "<td align=\"center\" valign=\"top\">" . (($nextMonth == "") ? "&nbsp;" : "<a href=\"$nextMonth\">&gt;&gt;</a>")  . "</td>\n";
    	$s .= "</tr>\n";
    	
    	$s .= "<tr>\n";
    	$s .= "<td align=\"center\" valign=\"top\" class=\"calendarHeader\">" . $this->dayNames[($this->startDay)%7] . "</td>\n";
    	$s .= "<td align=\"center\" valign=\"top\" class=\"calendarHeader\">" . $this->dayNames[($this->startDay+1)%7] . "</td>\n";
    	$s .= "<td align=\"center\" valign=\"top\" class=\"calendarHeader\">" . $this->dayNames[($this->startDay+2)%7] . "</td>\n";
    	$s .= "<td align=\"center\" valign=\"top\" class=\"calendarHeader\">" . $this->dayNames[($this->startDay+3)%7] . "</td>\n";
    	$s .= "<td align=\"center\" valign=\"top\" class=\"calendarHeader\">" . $this->dayNames[($this->startDay+4)%7] . "</td>\n";
    	$s .= "<td align=\"center\" valign=\"top\" class=\"calendarHeader\">" . $this->dayNames[($this->startDay+5)%7] . "</td>\n";
    	$s .= "<td align=\"center\" valign=\"top\" class=\"calendarHeader\">" . $this->dayNames[($this->startDay+6)%7] . "</td>\n";
    	$s .= "</tr>\n";
    	
    	// We need to work out what date to start at so that the first appears in the correct column
    	$d = $this->startDay + 1 - $first;
    	while ($d > 1)
    	{
    	    $d -= 7;
    	}

      // Make sure we know when today is, so that we can use a different CSS style
      $today = getdate(time());
    	if(isset($idate))
    	{
    	  $today["mday"] = $idate["mday"];
    	  $today["mon"] = $idate["mon"];
    	  $today["year"] = $idate["year"];
    	}
    	while ($d <= $daysInMonth)
    	{
    	    $s .= "<tr>\n";       
    	    
    	    for ($i = 0; $i < 7; $i++)
    	    {
        	    $class = ($year == $today["year"] && $month == $today["mon"] && $d == $today["mday"]) ? "calendarToday" : "calendarDay";
    	        $s .= "<td class=\"$class\" align=\"center\" valign=\"top\">";       
    	        if ($d > 0 && $d <= $daysInMonth)
    	        {
    	            $link = $this->getDateLink($d, $month, $year, $tgt);
    	            $s .= (($link == "") ? $d : "<a href=\"$link\">$d</a>");
    	        }
    	        else
    	        {
    	            $s .= "&nbsp;";
    	        }
      	        $s .= "</td>\n";       
        	    $d++;
    	    }
    	    $s .= "</tr>\n";    
    	}
    	
    	$s .= "</table>\n";
    	
    	return $s;  	
    }
    
    
    /*
        Generate the HTML for a given year
    */
    function getYearHTML($year)
    {
      global $tgt;
        $s = "";
    	$prev = $this->getCalendarLink(0, $year - 1, $tgt);
    	$next = $this->getCalendarLink(0, $year + 1, $tgt);
        
        $s .= "<table class=\"calendar\" border=\"0\">\n";
        $s .= "<tr>";
    	$s .= "<td align=\"center\" valign=\"top\" align=\"left\">" . (($prev == "") ? "&nbsp;" : "<a href=\"$prev\">&lt;&lt;</a>")  . "</td>\n";
        $s .= "<td class=\"calendarHeader\" valign=\"top\" align=\"center\">" . (($this->startMonth > 1) ? $year . " - " . ($year + 1) : $year) ."</td>\n";
    	$s .= "<td align=\"center\" valign=\"top\" align=\"right\">" . (($next == "") ? "&nbsp;" : "<a href=\"$next\">&gt;&gt;</a>")  . "</td>\n";
        $s .= "</tr>\n";
        $s .= "<tr>";
        $s .= "<td class=\"calendar\" valign=\"top\">" . $this->getMonthHTML(0 + $this->startMonth, $year, 0) ."</td>\n";
        $s .= "<td class=\"calendar\" valign=\"top\">" . $this->getMonthHTML(1 + $this->startMonth, $year, 0) ."</td>\n";
        $s .= "<td class=\"calendar\" valign=\"top\">" . $this->getMonthHTML(2 + $this->startMonth, $year, 0) ."</td>\n";
        $s .= "</tr>\n";
        $s .= "<tr>\n";
        $s .= "<td class=\"calendar\" valign=\"top\">" . $this->getMonthHTML(3 + $this->startMonth, $year, 0) ."</td>\n";
        $s .= "<td class=\"calendar\" valign=\"top\">" . $this->getMonthHTML(4 + $this->startMonth, $year, 0) ."</td>\n";
        $s .= "<td class=\"calendar\" valign=\"top\">" . $this->getMonthHTML(5 + $this->startMonth, $year, 0) ."</td>\n";
        $s .= "</tr>\n";
        $s .= "<tr>\n";
        $s .= "<td class=\"calendar\" valign=\"top\">" . $this->getMonthHTML(6 + $this->startMonth, $year, 0) ."</td>\n";
        $s .= "<td class=\"calendar\" valign=\"top\">" . $this->getMonthHTML(7 + $this->startMonth, $year, 0) ."</td>\n";
        $s .= "<td class=\"calendar\" valign=\"top\">" . $this->getMonthHTML(8 + $this->startMonth, $year, 0) ."</td>\n";
        $s .= "</tr>\n";
        $s .= "<tr>\n";
        $s .= "<td class=\"calendar\" valign=\"top\">" . $this->getMonthHTML(9 + $this->startMonth, $year, 0) ."</td>\n";
        $s .= "<td class=\"calendar\" valign=\"top\">" . $this->getMonthHTML(10 + $this->startMonth, $year, 0) ."</td>\n";
        $s .= "<td class=\"calendar\" valign=\"top\">" . $this->getMonthHTML(11 + $this->startMonth, $year, 0) ."</td>\n";
        $s .= "</tr>\n";
        $s .= "</table>\n";
        
        return $s;
    }

    /*
        Adjust dates to allow months > 12 and < 0. Just adjust the years appropriately.
        e.g. Month 14 of the year 2001 is actually month 2 of year 2002.
    */
    function adjustDate($month, $year)
    {
        $a = array();  
        $a[0] = $month;
        $a[1] = $year;
        
        while ($a[0] > 12)
        {
            $a[0] -= 12;
            $a[1]++;
        }
        
        while ($a[0] <= 0)
        {
            $a[0] += 12;
            $a[1]--;
        }
        
        return $a;
    }

    /* 
        The start day of the week. This is the day that appears in the first column
        of the calendar. Sunday = 0.
    */
    var $startDay = 0;

    /* 
        The start month of the year. This is the month that appears in the first slot
        of the calendar in the year view. January = 1.
    */
    var $startMonth = 1;

    /*
        The labels to display for the days of the week. The first entry in this array
        represents Sunday.
    */
    var $dayNames = array("S", "M", "T", "W", "T", "F", "S");
    
    /*
        The labels to display for the months of the year. The first entry in this array
        represents January.
    */
    //var $monthNames = array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
    
    /*
        The number of days in each month. You're unlikely to want to change this...
        The first entry in this array represents January.
    */
    var $daysInMonth = array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
    
} // end class Calendar

// use this one to get arrows for prev - next months
class MyCalendar extends Calendar
{
  function getCalendarLink($month, $year, $tgt)
  {
    // Redisplay the current page, but with some parameters
    // to set the new month and year
    $s = $_SERVER['PHP_SELF'];
    return "$s?month=$month&year=$year&tgt=$tgt";
  }
}  // end class MyCalendar

/*
   I n s t a n t i a t o r
   
   Create an instance of the calendar
*/
// Prepare data inputs:

// If no month/year is given, use current month/year
$d = getdate(time());    // get ready to fake the month/year if none present
$month = $_GET["month"]; // if present, the desired month
$year = $_GET["year"];   // if present, the desired year
$tgt = $_GET["tgt"];     // window opener's form field the date should be returned to
if(!isset($_GET['id']) or !isset($_GET['tbl']) or !isset($_GET['fld']))
{
  if ($month == "")
  {
    $month = $d["mon"];  // current month, if none given
  }
  if ($year == "")
  {
    $year = $d["year"];  // current year, if none given
  }
} else {
  // retrieve a known date (as in editing an event or article)
  $id = $_GET['id'];
  $tbl = $_GET['tbl'];  // probably Content or Events
  $fld = $_GET['fld'];  // probably Begin, Expires or Date
// log in to the database
if (!($db = @ mysql_connect($hostName, $userName, $passWord)))
   showError();

if (!( @ mysql_select_db($databaseName,$db)))
   showError();

  $sql = "SELECT $fld FROM $tbl WHERE Id='$id' LIMIT 1";
  $result = mysql_query($sql, $db);
  $myrow = mysql_fetch_array($result);
  $mydate = getdate(strtotime($myrow[$fld]));
  $month = $mydate["mon"];
  $year = $mydate["year"];
}

// Construct an interactive calendar of the current month
 $cal = new MyCalendar;
 echo "<div id=\"calendarBox\" align=\"center\">\n";
 echo "<div style=\"margin: 0 auto;\">";
 echo $cal->getMonthView($month, $year, $tgt);
 echo "</div>\n";
 echo "</div>\n";
?>
</body>
</html>
Return current item: Open Chamber of Commerce