カレンダーを表示する
... PHP > オリジナルfunction
written 2010/6/17
一般的な表スタイルの月間カレンダーを表示します。これを基本にして、スケジューラなどを作成することができます。
<?php
$cur_y = date("Y");
$cur_m = date("m");
//曜日を示す漢字の配列
$weekAr = array("日","月","火", "水", "木","金","土");
//月の初日
$monthFirst = strtotime($cur_y ."/" .$cur_m ."/1");
//月末
$monthLast = strtotime($cur_y ."/".$cur_m ."/" .date("t", $monthFirst));
print "<h1>" .date("Y年n月", $monthFirst) ."</h1>¥n";
print "<table width=¥"100%¥" rules=¥"all¥">¥n";
//カレンダー上部の曜日の行を出力する
print "<tr>¥n";
foreach($weekAr as $weekStr){
print "<th>" .$weekStr . "</th>¥n";
}
print "</tr>¥n";
//月の初日までに空白セルが何個必要か
$spaceNumFirst = date("w",$monthFirst);
//月末部分に空白セルが何個必要か
$spaceNumLast = 6 - date("w",$monthLast);
$nowW = 0;//現在のセルの曜日を示す変数
print "<tr>¥n";
if($spaceNumFirst > 0){//月初めの空白セルを出力
for($i=0;$i<$spaceNumFirst;$i++){
print "<td>-</td>¥n";
$nowW++;
}
}
//1日から月末まで出力開始
for($i=1;$i<=date("t", $monthFirst);$i++){
print "<td>" .$i ."</td>¥n";
if($i == date("d", $monthLast)) break;
if($nowW == 6){
print "</tr>¥n<tr>¥n";
$nowW = 0;
}else{
$nowW++;
}
}
if($spaceNumLast > 0){//月末部分の空白セルを出力
for($i=0;$i<$spaceNumLast;$i++){
print "<td>-</td>¥n";
}
}
print "</tr>¥n";
print "</table>¥n";
?>
【出力結果】
注)バックスラッシュは全角の¥で表記しています。
現在のカテゴリ :
... PHP > オリジナルfunction


利用上の注意事項
スレッドを見る
記号








