指定月のカレンダーの作成

Perl Script make_calender.cgi


#------------------#
#  カレンダー本体  #
#------------------#

sub make_calender {

      my ($year, $month) = @_;
      my ($i, $temp);

      # 曜日を定義
      my @week = ("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat");

      # 各月の総日数
      my (@mday) = (31,28,31,30,31,30,31,31,30,31,30,31);

      # 閏年の判定(閏年だったら二月を29日にする)
      if ($year%4 == 0 && $year%100 != 0  || $year%400 == 0) {
            $mday[1] = 29;
            }

      # 指定された月の最初の日の曜日を求める(Zellerの公式)
      my ($firstday) = &zeller($year, $month, 1);

      # 指定された月の総日数を代入
      my ($maxday) = $mday[$month-1];

      # カレンダーの出力開始
      print "<table summary="Calender">\n";

      # 曜日を入れる
      print "<tr>\n";
      for ($i=0; $i<7; $i++) {
            # 曜日名のクラスをつける
            print "<td class="$week[$i]">$week[$i]</td>\n";

            }
      print "</tr>\n";

      # 日付がないセルを処理(指定月の1日が日曜以外のとき)
      if ($firstday != 0) {

            print "<tr>\n";
            for ($i=0; $i<$firstday; $i++) {
                  print "<td class="$week[$temp]">-</td>\n";
                  $temp++;
                  }

            }


      # 日付を入れる
      for ($i=1; $i<=$maxday; $i++) {

            if (!$temp) {
                  print "<tr>\n";
                  print "<td class="$week[$temp]">$i</td>\n";
                  } else {
                  print "<td class="$week[$temp]">$i</td>\n";
                  }

            $temp++;

            if($temp == 7){ 
                  $temp = 0;
                  print "</tr>\n";
                  }

            }

      # 日付がない残りのセルを処理

      # $tempが真=最終日が土曜日ではない
      if ($temp) {

            for ($i=$temp; $i<7; $i++) {
                  print "<td class="$week[$i]">-</td>\n";
                  }
            print "</tr>\n";

            }

      print "</table>\n";

}

#----------------#
#  Zellerの公式  #
#----------------#

sub zeller {

      my ($zy, $zm, $zd) = @_;
      my ($ans, $C, $Y, @ty);

      if ($zm == 1 || $zm == 2) {
            $zm  += 12;
            $zy--;
            }

      @ty = split("", $zy);
      $C = "$ty[0]$ty[1]";
      $Y = "$ty[2]$ty[3]";

      $ans = (int(21*$C/4) + int(5*$Y/4) + int(26*($zm+1)/10) + ($zd-1)) % 7;

      return $ans;

}

使い方

カレンダー表示
&make_calender(西暦, 月);
返り値:なし
Zellerの公式
&zeller(西暦, 月, 日);
返り値:0〜6 (日曜を0とする)

特定の月で曜日がおかしくなる不具合を修正しました。
どうも、Zellerの公式の記述に問題があったようです。

表示例 : 2004年9月のカレンダー &make_calender(2004, 9)

Sun Mon Tue Wed Thu Fri Sat
- - - 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 - -

*サーバーでCGIが使えないため別サーバーで出力したHTMLを表示しています。
*CSSを適用済みです。 calender_style.css

使用例別サーバーを使用

*数字を入力すると別サーバーに情報を送信してカレンダーを表示します。
*サーバーが落ちている場合が多々あります。