phpでカレンダー表示するよ

iyamaです。

今日はphp。 カレンダー表示をするクラスをご紹介します。

今日のコードは、どなたかが作ったクラスを 自分の使いたいようにアレンジしたclassです。

環境

  • PHP 5.x
CalendarClass.php
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/**
 * Calendar Class
 */
class Calendar {
  var $year = 0;
  var $month = 0;
  var $home = null;

  /**
  * Calendar
  * @param   $year       年
  * @param   $month      月
  */
   function Calendar($year = 0, $month = 0) {
      if (!checkdate($month, 1, $year)) {
          $year = date('Y');
          $month = date('m');
      }

      $this->year = (int)$year;
      $this->month = (int)$month;
  }

  /**
  * _days
  * 当月の日数取得(閏年対応)
  */
  function _numDays() {
      return date('d', mktime(0, 0, 0, $this->month + 1, 0, $this->year));
  }

  /**
  * _first_day
  * 当月初めの曜日
  */
   function _firstDay() {
      return date('w', mktime(0, 0, 0, $this->month, 1, $this->year));
  }

  /**
  * _last_day
  * 当月終わりの曜日
  */
   function _lastDay() {
      return date('w', mktime(0, 0, 0, $this->month, $this->_numDays(), $this->year));
  }


  /**
  * makeCalendar
  * カレンダーの内容表示
  */
  function makeCalendar() {
      // init
      $days = (int)$this->_numDays();
      $firstDay = (int)$this->_firstDay();
      $lastDay = (int)$this->_lastDay();

      $lastWeekDays = ($days + $firstDay) % 7;
      if ($lastWeekDays == 0) {
          $weeks = ($days + $firstDay) / 7;
      } else {
          $weeks = ceil(($days + $firstDay) / 7);
      }

      // view
      $i = 0;
      $j = 0;
      $day = 0;
      while ($i < $weeks) {
          $j = 0;
          $calendar .= "<tr>\r\n";
          while ($j < 7) {
              if (($i == 0 && $j < $firstDay) || ($i == $weeks - 1 && $j > $lastDay)) {
                  // 日付外のセルにあたること。だいたい空白を表示させる。
                  $calendar .= "\t<td>&nbsp;</td>\r\n";
              } else {
                  $day++;
                  // $dayが日付
                  // ここでその日に表示させたい処理を行う
                  $calendar .= "\t<td>{$day}</td>\r\n";
              }
              $j++;
          }
          $calendar .= "</tr>\r\n";
          $i++;
      }

      return $calendar;
  }

}

当月のカレンダー表示をします。 週のチェックは$jでやってます。 こんな複雑にやらなくてもいいような気がするけど、 このスタイルが一番、カスタマイズしやすかったです。 で、このクラスを使うときは以下のようにしてください。

index.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<?php
require_once('CalendarClass.php');

list($year, $month) = explode(' ', date("Y n"));
$calendar = new Calendar($year, $month);
?>

<!-- ▼▼calendar▼▼ -->
<table>
  <tr><td colspan="7"><?php echo "{$year}{$month} 月"; ?></td></tr>
  <tr>
      <td>日</td>
      <td>月</td>
      <td>火</td>
      <td>水</td>
      <td>木</td>
      <td>金</td>
      <td>土</td>
  </tr>
  <?php echo $calendar->makeCalendar(); ?>
</table>
<!-- ▲▲calendar▲▲ -->

デザインとゆーか、htmlとなるべく分離したいと思うんだけど、 このくらいが限界です。。

以上です。

Comments