カレンダー(前月・次月へのリンク付)
#! /usr/local/bin/perl loadForm(); # フォームデータの取り込み if($FORM{'y'} and $FORM{'m'}) { $y = $FORM{'y'}; # 年 $m = $FORM{'m'}; # 月 $d = 1; if($m <= 2) { $y--; $m += 12; } $startwday = ($y + int($y / 4) - int($y / 100) + int($y / 400) + int((13 * $m + 8) / 5) + $d) % 7; if($m > 12) { $y++; $m -= 12; } } else { $time = time; ($sec, $min, $hour, $d, $m, $y, $w) = localtime($time); # 今日の年月取得 $y += 1900; # 今日の年 $m++; # 今日の月 $startwday = ($w - $d % 7 + 1 + 7) % 7; # 今月1日の曜日 } @days = (31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31); if($m == 2 and ($y % 4 == 0 and $y % 100 != 0 or $y % 400 == 0)) { $days[1]++; # うるう年 } $enddate = $days[$m - 1]; # 月の日数 $prevyear = $y; # 前月・次月 $prevmon = $m - 1; $nextyear = $y; $nextmon = $m + 1; if($prevmon < 1) { $prevyear -= 1; $prevmon += 12; } if($nextmon > 12) { $nextyear += 1; $nextmon -= 12; } print qq(Content-type: text/html; charset=Shift_JIS\n\n); print <<END; <html> <head> <title>カレンダー $y年$m月</title> </head> <body> <center> <table border="5" bordercolor="#99ff99" cellspacing="0" cellpadding="8"> <tr> <th colspan="7"> <a href="$ENV{'SCRIPT_NAME'}?y=$prevyear&m=$prevmon" style="font-size:xx-small">≪前月</a> $y<span style="font-size:xx-small">年</span>$m<span style="font-size:xx-small">月</span> <a href="$ENV{'SCRIPT_NAME'}?y=$nextyear&m=$nextmon" style="font-size:xx-small">次月≫</a> </th> </tr> <tr> <td style="font-size:x-small;color:red">日</td> <td style="font-size:x-small">月</td> <td style="font-size:x-small">火</td> <td style="font-size:x-small">水</td> <td style="font-size:x-small">木</td> <td style="font-size:x-small">金</td> <td style="font-size:x-small;color:blue">土</td> </tr> END for($i = 0 ; $i < $startwday ; $i++) { # 1日までの空欄 if(($count % 7) == 0) { print qq(<tr>); } print qq(<td> </td>); $count++; } for($i = 1 ; $i <= $enddate ; $i++) { # 日付を書き込む if(($count % 7) == 0) { print qq(<tr>); } print qq(<td align="right"); if(($count % 7) == 0) { print qq( style="color:red"); } if(($count % 7) == 6) { print qq( style="color:blue"); } print qq(>$i); print qq(</td>); $count++; if(($count % 7) == 0) { print qq(</tr>\n); } } for( ; ($count % 7) != 0 ; ) { # 最後の日からの空欄 print qq(<td> </td>); $count++; if(($count % 7) == 0) { print qq(</tr>\n); } } print <<END; </table> </center> </body> </html> END exit; #============================================ フォームデータ取り込み sub loadForm { if($ENV{'REQUEST_METHOD'} eq 'POST') { read(STDIN, $query, $ENV{'CONTENT_LENGTH'}); } else { $query = $ENV{'QUERY_STRING'}; } foreach $pair (split(/&/, $query)) { ($key, $value) = split(/=/, $pair); $FORM{$key} = $value; } }
〔 実行する 〕