summaryrefslogtreecommitdiff
path: root/calendar.class.php
blob: 9ac2f0787d9f05c7e8457253020fbb20aadcd996 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
<?

class calendar {
   var $date;

   function calendar($date) {
     $this->date = $date;
   }

  function display() {
    global $PHP_SELF;

    ### Extract information from the given date:
    $month  = date("n", $this->date);
    $year = date("Y", $this->date);
    $day = date("d", $this->date);

    ### Extract first day of the month:
    $first = date("w", mktime(0, 0, 0, $month, 1, $year));
        
    ### Extract last day of the month:
    $last = date("t", mktime(0, 0, 0, $month, 1, $year));

    ### Calculate previous and next months dates:
    $prev = mktime(0, 0, 0, $month - 1, $day, $year);
    $next = mktime(0, 0, 0, $month + 1, $day, $year);

    ### Generate calendar header:
    $output .= "<TABLE WIDTH=\"150\" BORDER=\"1\" CELLSPACING=\"0\" CELLPADDING=\"2\">";
    $output .= " <TR><TH COLSPAN=\"7\"><A HREF=\"$PHP_SELF?date=$prev\">&lt;&lt;</A> &nbsp; ". date("F Y", $this->date) ." &nbsp; <A HREF=\"$PHP_SELF?date=$next\">&gt;&gt;</A></TH></TR>";
    $output .= " <TR><TH>S</TH><TH>M</TH><TH>T</TH><TH>W</TH><TH>T</TH><TH>F</TH><TH>S</TH></TR>\n";
 
    ### Initialize temporary variables:
    $nday = 1;
    $sday = $first;
   
    ### Loop through all the days of the month:
    while ($nday <= $last) {
      ### Set up blank days for first week of the month:
      if ($first) {
        $output .= "<TR><TD COLSPAN=\"$first\">&nbsp</TD>";
        $first = 0;
      }
        
      ### Start every week on a new line:
      if ($sday == 0) $output .=  "<TR>";
    
      ### Print one cell:
      $date = mktime(0, 0, 0, $month, $nday, $year);
      if ($nday == $day) $output .= "<TD ALIGN=\"center\"><B>$nday</B></TD>";
      else if ($date > time()) $output .= "<TD ALIGN=\"center\">$nday</TD>";
      else $output .= "<TD ALIGN=\"center\"><A HREF=\"$PHP_SELF?date=$date\">$nday</A></TD>";
     
      ### Start every week on a new line:
      if ($sday == 6) $output .=  "</TR>";
        
      ### Update temporary variables:
      $sday++;
      $sday = $sday % 7;
      $nday++;
    }
    
    ### End the calendar:
    if ($sday != 0) {
      $end = 7 - $sday;
      $output .= "<TD COLSPAN=\"$end\">&nbsp;</TD></TR>";
    }
    $output .= "</TABLE>";

    ### Return calendar:
    return $output;
  }
}

?>