カレンダー(今年1月~12月, 前年次年リンク付)
<?php
$min = 1902;
$max = 2037;
if(preg_match("/^\/\d+\/$/", $_SERVER['PATH_INFO'])) {
$year = $_SERVER['PATH_INFO'];
$year = preg_replace("/^\//", "", $year);
$year = preg_replace("/\/$/", "", $year);
if($year < $min or $year > $max) {
$year = date("Y");
}
}
else {
$year = date("Y");
}
$prev = $year - 1;
if($prev < $min) {
$prev = date("Y");
}
$next = $year + 1;
if($next > $max) {
$next = date("Y");
}
$scriptname = $_SERVER['SCRIPT_NAME'];
$scriptname = preg_replace("/\.php$/", "", $scriptname);
?>
<html>
<head>
<title><?=$year?>年カレンダー</title>
</head>
<body>
<table cellpadding="5" align="center">
<?php
echo "<tr>\n",
"<td><a href=\"$scriptname/$prev/\">$prev ≪</a></td>\n",
"<th><font size=\"+2\">$year</font>年</th>\n",
"<td align=\"right\"><a href=\"$scriptname/$next/\">≫ $next</a></td>\n",
"</tr>\n";
for($m = 1 ; $m <= 12 ; $m++) { // 1月~12月のカレンダーを表示
$time = mktime(0, 0, 0, $m, 1, $year);
$startwday = date("w", $time); // 1日の曜日
$endmday = date("t", $time); // 最後の日
$count = 0;
if($m % 3 == 1) {
echo "<tr>\n";
}
echo "<td valign=\"top\">\n",
"<table border=\"3\" bordercolor=\"#333399\" cellspacing=\"0\" cellpadding=\"5\">\n",
"<tr>\n",
"<th colspan=\"7\"><font size=\"+2\">$m</font><font size=\"-1\">月</font></th>\n",
"</tr>\n",
"<tr>\n",
"<td align=\"center\"><font size=\"-1\" color=\"red\">日</font></td>\n",
"<td align=\"center\"><font size=\"-1\">月</font></td>\n",
"<td align=\"center\"><font size=\"-1\">火</font></td>\n",
"<td align=\"center\"><font size=\"-1\">水</font></td>\n",
"<td align=\"center\"><font size=\"-1\">木</font></td>\n",
"<td align=\"center\"><font size=\"-1\">金</font></td>\n",
"<td align=\"center\"><font size=\"-1\" color=\"blue\">土</font></td>\n",
"</tr>\n";
for($i = 0 ; $i < $startwday ; $i++) { // 1日までの空欄
if(($count % 7) == 0) {
echo "<tr>";
}
echo "<td> </td>";
$count++;
}
for($i = 1 ; $i <= $endmday ; $i++) { // 日付を書き込む
if(($count % 7) == 0) {
echo "<tr>";
}
echo "<td align=\"right\">";
if(($count % 7) == 0) { // 日曜日の場合、文字色を赤
echo "<font color=\"red\">";
}
elseif(($count % 7) == 6) { // 土曜日の場合、文字色を青
echo "<font color=\"blue\">";
}
echo "$i";
if(($count % 7) == 0 or ($count % 7) == 6) {
echo "</font>";
}
echo "</td>";
$count++;
if(($count % 7) == 0) {
echo "</tr>\n";
}
}
for( ; $count < 42 ; ) { // 最後の日からの空欄
if(($count % 7) == 0) {
echo "<tr>";
}
echo "<td> </td>";
$count++;
if(($count % 7) == 0) {
echo "</tr>\n";
}
}
echo "</table>\n",
"</td>\n";
if($m % 3 == 0) {
echo "</tr>\n";
}
}
?>
</table>
</body>
</html>
〔 実行する 〕