カレンダー(今年1月~12月)
#! /usr/local/bin/perl
($sec, $min, $hour, $date, $mon, $year, $wday, $yday) = localtime(time); # 今日
$year += 1900;
@days = (31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31); # 月の日数
if($year % 4 == 0 and $year % 100 != 0 or $year % 400 == 0) {
$days[1]++; # うるう年
}
$startwday = ($wday - $yday % 7 + 7) % 7; # 今年1月1日の曜日
print qq(Content-type: text/html; charset=UTF-8\n\n);
print <<END;
<html>
<head>
<title>$year年カレンダー</title>
</head>
<body>
<center>
<table cellpadding="5">
END
for($m = 1 ; $m <= 12 ; $m++) { # カレンダー表示(1~12)
my $count = 0;
if($m % 3 == 1) {
print qq(<tr>\n);
}
print qq(<td valign="top">\n);
print qq(<table border="5" bordercolor="#cccc66" cellspacing="0" cellpadding="2">\n);
print qq(<tr>\n);
print qq( <th colspan="7"><font size="+1">$m</font><font size="-1">月</font></th>\n);
print qq(</tr>\n);
print qq(<tr>\n);
print qq( <td><font size="-1" color="red">日</font></td>\n);
print qq( <td><font size="-1">月</font></td>\n);
print qq( <td><font size="-1">火</font></td>\n);
print qq( <td><font size="-1">水</font></td>\n);
print qq( <td><font size="-1">木</font></td>\n);
print qq( <td><font size="-1">金</font></td>\n);
print qq( <td><font size="-1" color="blue">土</font></td>\n);
print qq(</tr>\n);
for($i = 0 ; $i < $startwday ; $i++) { # 1日までの空欄
if(($count % 7) == 0) {
print qq(<tr>);
}
print qq(<td> </td>);
$count++;
}
for($i = 1 ; $i <= $days[$m - 1] ; $i++) { # 日付表示
if(($count % 7) == 0) {
print qq(<tr>);
}
print qq(<td align="right">);
if(($count % 7) == 0) {
print qq(<font color="red">);
}
if(($count % 7) == 6) {
print qq(<font color="blue">);
}
print qq($i);
if(($count % 7) == 0 or ($count % 7) == 6) {
print qq(</font>);
}
print qq(</td>);
$count++;
if(($count % 7) == 0) {
print qq(</tr>\n);
}
}
$startwday = $count % 7; # 次月1日の曜日
for( ; ($count % 7) != 0 ; ) { # 最後の日からの空欄
print qq(<td> </td>);
$count++;
if(($count % 7) == 0) {
print qq(</tr>\n);
}
}
print qq(</table>\n);
print qq(</td>\n);
if(($m + 1) % 3 == 1) {
print qq(</tr>\n);
}
}
print <<END;
</table>
</center>
</body>
</html>
END
exit;
〔 実行する 〕