#native_company# #native_desc#
#native_cta#

Calender Classes

By Stan Lemon
on July 25, 2003

Version: 2.6

Type: Class

Category: Calendars/Dates

License: GNU General Public License

Description: Contains a core class with no visual interface for use by you in your scripts AND a HTML interface class that will use the core class to display a one month calender with drop down month & year selector, next/previous month buttons, dates displayed are clickable and highlightable.

Currently it doesn’t look so great in netscape so I would appreciate any assistance with that.

<?php

/* 

This an extension to the class I wrote, v 2.5.1 written by: David Hernandez Sanz
([email protected]) http://www.dhs.sitio.net/

This class is intended to be an extension, so make sure you download 2.5.1 first.

It includes a lot of improvements, and also allows for some european calendar option.

*/

//require("cal.php");

class Calendar1 extends Calendar
{
  var $dayBeginWeek = 1; // Day begin of week (for example, 1 for UK or 2 for Spain)

  var $minMonth = 2;    // Valid date interval for mktime()
  var $minYear  = 1970;
  var $maxMonth = 12;
  var $maxYear  = 2037;

  function startCalendar($myCal) {
    if (!(null === $myCal["month"]) && !(null === $myCal["year"])) {
      $thecalendar = $this->displayCalendar($myCal);
    }
    else {
        $now = date('n,Y');
      $myDate = explode(",",$now);
      $myCal["month"] = $myDate[0];
      $myCal["year"] = $myDate[1];
      $thecalendar = $this->displayCalendar($myCal);
    }

    return $thecalendar;
  }

  function checkDate($month,$year) {
    if (!is_numeric($month) || !is_numeric($year))
      $txt = $this->myError("Either the month of year was non-numeric.");
    elseif ($month > 12 || $month < 1 || ((1 == $month) && ($this->minYear == $year)))
      $txt = $this->myError("The number passed for the month value is not valid.");
    elseif ($year > $this->maxYear || $year < $this->minYear)
      $txt = $this->myError("The number passed for the year value is not valid.");
    else
      $txt = null;
    return $txt;
  }

  function next($month, $year) {
    if (($this->maxMonth == $month) && ($this->maxYear == $year))
      $next = null;
    elseif (12 == $month) {
      $next["month"] = 1;
      $next["year"] = $year + 1;
    }
    else {
      $next["month"] = $month + 1;
      $next["year"] = $year;
    }

    return $next;
  }

  function last($month,$year) {
    if (($this->minMonth == $month) && ($this->minYear == $year))
      $last = null;
    elseif (1 == $month) {
      $last["month"] = 12;
      $last["year"] = $year - 1;
    }
    else {
      $last["month"] = $month - 1;
      $last["year"] = $year;
    }

    return $last;
  }

	function dayName($day) {
		$dayName = array(1 => "Sun",
						   2 => "Mon",
						   3 => "Tue",
						   4 => "Wed",
						   5 => "Thu",
						   6 => "Fri",
						   7 => "Sat");

		return $dayName[$day];
	}

  function displayCalendar($myCal)
  {
    global $_SERVER;

    extract($myCal);

    // Make sure that we have a valid month and year.
    $check = $this->checkDate($month,$year);

    if (null == $check)
    {
      $next = ($link_next) ? $this->next($month, $year) : null;
      $last = ($link_last) ? $this->last($month, $year) : null;
      if (!$url_nav)
        $url_nav = $_SERVER["PHP_SELF"];
      $parse = parse_url($url_nav);
      $sep = $parse["query"] ? "&" : "?";
      if ($last)
      {
        $url = $url_nav . $sep . "month=" . $last["month"] . "&year=" . $last["year"];
        $url = htmlentities($url, ENT_QUOTES);
        $txt_last = "<a href="$url" class="$class">&lt;&lt;</a>";
      }
      else
        $txt_last = "&nbsp;";
      if ($next)
      {
        $url = $url_nav . $sep . "month=" . $next["month"] . "&year=" . $next["year"];
        $url = htmlentities($url, ENT_QUOTES);
        $txt_next = "<a href="$url" class="$class">&gt;&gt;</a>";
      }
      else
        $txt_next = "&nbsp;";

      $myCalendar = "<table border="1" cellpadding="0" cellspacing="0" class="$class">n" .
                    "  <tr>n" .
                    "    <td colspan="7" align="center">n" .
                    "      <table border="0" cellpadding="1" cellspacing="0" width="100%" class="$class">n" .
                    "        <tr>n" .
                    "          <th align="center" width="30" class="$class_nav">n" .
                    "            $txt_last</th>n" .
                    "          <th align="center" class="$class_header">n" .
                    "            " . $this->monthName($month) . " $year</th>n" .
                    "          <th align="center" width="30" class="$class_nav">n" .
                    "            $txt_next</th>n" .
                    "        </tr>n" .
                    "      </table></td>n" .
                    "  </tr>n" .
                    "  <tr>n";

      for ($i = 0; $i < 7; $i++)
      {
        $i_real = $i + $this->dayBeginWeek;
        if (8 <= $i_real)
          $i_real -= 7;
        $myCalendar .= "    <td width="27" align="center" valign="middle" class="$class">n" .
                       "      " . $this->dayName($i_real) . "</td>n";
      }
      $myCalendar .= "  </tr>n";

      $day = null;

      $first_day_of_month = $this->getDayOfWeek(1, $month, $year);
      $first_c_of_month = $first_day_of_month + 1 - $this->dayBeginWeek;
      if (0 > $first_c_of_month)
        $first_c_of_month += 7;

      if (!$url_day)
        $url_day = $_SERVER["PHP_SELF"];
      $parse = parse_url($url_day);
      $sep = $parse["query"] ? "&" : "?";

      //scan thru calendar grid
      for ($r = 1; $r <= 6; $r++)
        for ($c = 0; $c < 7; $c++) {
          if (0 == $c)
            $myCalendar .= "  <tr>n";
          if ((null === $day) && ($c < $first_c_of_month))
            $myCalendar .= "    <td class="$class_empty">n" .
                           "      &nbsp;</td>n";
          elseif ($day && ($this->daysInMonth($month, $year) < $day)) {
            $myCalendar .= "    <td class="$class_empty">n" .
                           "      &nbsp;</td>n";
            $r = 7;
          }
          else {
            if (null === $day)
              $day = 1;

            $day_week = $c + $this->dayBeginWeek;
            if (7 < $day_week)
              $day_week -= 7;
            if ($holiday[$day_week])
              $myCalendar .= "    <td align="center" valign="middle" class="$class_holiday">n" .
                             "      $day</td>n";
            elseif ((date(d) == $day) && (date(m) == $month) &&
                    (date(Y) == $year)) {
              $url = $url_day . $sep . "day=$day&month=$month&year=$year";
              $url = htmlentities($url, ENT_QUOTES);
              $myCalendar .= "    <td align="center" valign="middle" class="$class_today">n" .
                             "      <a href="$url" class="$class">n" .
                             "        $day</a></td>n";
            }
            else {
              $url = $url_day . $sep . "day=$day&month=$month&year=$year";
              $url = htmlentities($url, ENT_QUOTES);
              $myCalendar .= "    <td align="center" valign="middle" class="$class_normal">n" .
                             "      <a href="$url" class="$class">n" .
                             "        $day</a></td>n";
            }
            $day++;
          }
          if (6 == $c) {
            $myCalendar .= "  </tr>n";
            if ($this->daysInMonth($month, $year) < $day)
              $r = 7;
          }
        }
      $myCalendar .= "</table>n";
    }
    else
      $myCalendar = $check;
    return $myCalendar;
  }

}



/*
// First configure this bad boy.

echo '<style>
TABLE.calendario
{
  FONT-FAMILY: tahoma, arial, sans-serif;
}
TH.calendarioCab
{
  BACKGROUND-COLOR: #DDDDDD;
  COLOR: #000000;
  FONT-SIZE: 100%;
  FONT-WEIGHT: normal;
}
TH.calendarioNav
{
  BACKGROUND-COLOR: #DDDDDD;
  COLOR: #000000;
  FONT-SIZE: 80%;
  FONT-WEIGHT: normal;
}
TD.calendario
{
  BACKGROUND-COLOR: #BBBBBB;
  COLOR: #000000;
  FONT-SIZE: 80%;
}
TD.calendarioNormal
{
  BACKGROUND-COLOR: #EEEEEE;
  COLOR: "#0063C6";
  FONT-SIZE: 80%;
}
TD.calendarioHoy
{
  BACKGROUND-COLOR: #DDDDDD;
  COLOR: #000000;
  FONT-SIZE: 80%;
}
TD.calendarioFiesta
{
  BACKGROUND-COLOR: #CCCCCC;
  COLOR: #000000;
  FONT-SIZE: 80%;
}
TD.calendarioVacio
{
  BACKGROUND-COLOR: #FFFFFF;
  COLOR: #000000;
  FONT-SIZE: 80%;
}
A.calendario
{
  TEXT-DECORATION: none;
}
</style>

';

$myCal["class"] = "calendario";
$myCal["class_header"] = "calendarioCab";
$myCal["class_nav"] = "calendarioNav";
$myCal["class_normal"] = "calendarioNormal";
$myCal["class_today"] = "calendarioHoy";
$myCal["class_holiday"] = "calendarioFiesta";
$myCal["class_empty"] = "calendarioVacio";
$myCal["link_next"] = true;
$myCal["link_last"] = true;
$myCal["url_nav"] = $_SERVER["PHP_SELF"] . "?var=value";
$myCal["url_day"] = "page.php?var=value";
$myCal["holiday"] = array(1 => true,  2 => false, 3 => false, 4 => false, 5 => false, 6 => false, 7 => true);

extract($_GET);
if (!(isset($month) && isset($year))) {
  $month = null;
  $year = null;
}
$myCal = array_merge($myCal, compact("month", "year"));

// Start your engines!
$newCal = new Calendar1();
print $newCal->startCalendar($myCal);
*/
?>