http://q.hatena.ne.jp/1163814335
http://q.hatena.ne.jp/1164007516
で質問させて頂いて回答を頂きました。この問題でさらに機能をつけたく、例えば今日の日付を取得します。今日が11/23であれば
$gd=getdate();$today=$gd['mday']; で今日の日付を取得します。今日から月末までは$iにリンクをつけて(リンク先はなんでもOK)、1日から$todayまでの日付は日付($i)はリンクをつけません。リンクは"<td align=\"center\"><A href=XXX>$i</A></td>\n";につけます。このような条件文はどのようにしたらよいでしょうか?できれば下のforループを元にお教えください。
for( $i = 1 - $w ; $i <= $n + 7 ; $i++ ){
if ( ( ( $i + $w ) % 7 ) == 1 ) {
print "<tr>\n";
}
if ( ( 0 < $i ) && ( $i <= $n ) ) {
//土日色塗り判定
print "<td align=\"center\">$i</td>\n";
} else {
print "<td> </td>\n";
}
if ( ( ( $i + $w ) % 7 ) == 0 ) {
print "</tr>\n";
if ( $i >= $n ) {
break;
}
}
}
print "</table>\n";
カレンダーは今月のものを表示するという前提でしょうか。
日付の判定のみしています。
<html> <head> <title>PHPカレンダサンプル</title> <?php function drawCalender( $y ,$m ) { $t = mktime(0, 0, 0, $m, 1, $y); //$y年$m月1日のUNIXTIME $w = date('w', $t); //1日の曜日(0:日~6:土) $n = date('t', $t); //$y年$m月の日数 print '<table border="1" bgcolor="FFFFFF">'."\n"; print "<caption><font color=\"#0000FF\"><b>{$y}年{$m}月</font></b></caption>\n"; print <<<DAY_TITLE <tr> <th>日</th><th>月</th><th>火</th><th>水</th><th>木</th><th>金</th><th>土</th> </tr> DAY_TITLE; $gd=getdate(); $today=$gd['mday']; if ( ( ( $i + $w ) % 7 ) == 1 ) print "<tr>\n"; //曜日の色設定 switch( ( $i + $w ) % 7 ) { case 1 : $color = '"red"'; break; case 0 : $color = '"yellow"'; break; default : $color = '"white"'; break; } for( $i = 1 - $w ; $i <= $n + 7 ; $i++ ) { if ( ( ( $i + $w ) % 7 ) == 1 ) print "<tr>\n"; if ( ( 0 < $i ) && ( $i <= $n ) ) { print "<td bgcolor=$color align=\"center\">"; // リンク処理: // ↓↓↓↓↓↓↓↓↓↓↓↓↓↓ ここから if ( $i > $today ) { print '<A href="http://www.nnh.to/'.sprintf("%02d/%02d", $m, $i).'.html" target="_blank">'.$i."</A>"; } else { print $i; } // ↑↑↑↑↑↑↑↑↑↑↑↑↑↑ ここまで print "</td>\n"; } else { print "<td bgcolor=$color> </td>\n"; } if ( ( ( $i + $w ) % 7 ) == 0 ) { print "</tr>\n"; if ( $i >= $n ) break; } } print "</table>\n"; } ?> </head> <body bgcolor="#E0F0E0"> <center> <?php drawCalender( date('Y'), date('n') ); ?> </center> </body> </html>
もし、任意の月を表示するのであれば、
if ( $i > $today ) {
を
if mktime( 0,0,0,$m,$i,$y ) > mktime(0,0,0)
のように変更する必要があります。
カレンダーは今月のものを表示するという前提でしょうか。
日付の判定のみしています。
<html> <head> <title>PHPカレンダサンプル</title> <?php function drawCalender( $y ,$m ) { $t = mktime(0, 0, 0, $m, 1, $y); //$y年$m月1日のUNIXTIME $w = date('w', $t); //1日の曜日(0:日~6:土) $n = date('t', $t); //$y年$m月の日数 print '<table border="1" bgcolor="FFFFFF">'."\n"; print "<caption><font color=\"#0000FF\"><b>{$y}年{$m}月</font></b></caption>\n"; print <<<DAY_TITLE <tr> <th>日</th><th>月</th><th>火</th><th>水</th><th>木</th><th>金</th><th>土</th> </tr> DAY_TITLE; $gd=getdate(); $today=$gd['mday']; if ( ( ( $i + $w ) % 7 ) == 1 ) print "<tr>\n"; //曜日の色設定 switch( ( $i + $w ) % 7 ) { case 1 : $color = '"red"'; break; case 0 : $color = '"yellow"'; break; default : $color = '"white"'; break; } for( $i = 1 - $w ; $i <= $n + 7 ; $i++ ) { if ( ( ( $i + $w ) % 7 ) == 1 ) print "<tr>\n"; if ( ( 0 < $i ) && ( $i <= $n ) ) { print "<td bgcolor=$color align=\"center\">"; // リンク処理: // ↓↓↓↓↓↓↓↓↓↓↓↓↓↓ ここから if ( $i > $today ) { print '<A href="http://www.nnh.to/'.sprintf("%02d/%02d", $m, $i).'.html" target="_blank">'.$i."</A>"; } else { print $i; } // ↑↑↑↑↑↑↑↑↑↑↑↑↑↑ ここまで print "</td>\n"; } else { print "<td bgcolor=$color> </td>\n"; } if ( ( ( $i + $w ) % 7 ) == 0 ) { print "</tr>\n"; if ( $i >= $n ) break; } } print "</table>\n"; } ?> </head> <body bgcolor="#E0F0E0"> <center> <?php drawCalender( date('Y'), date('n') ); ?> </center> </body> </html>
もし、任意の月を表示するのであれば、
if ( $i > $today ) {
を
if mktime( 0,0,0,$m,$i,$y ) > mktime(0,0,0)
のように変更する必要があります。
毎回Mook様には助けられます。完璧な回答ありがとうございました!助かりました!!