PHPでカレンダーを作る
カレンダー用のライブラリーが色々と有りますので、表示するだけなら自作することもないのですが、プログラムに組み込んで色々とやりたいとき(予約システムのカレンダーなど)の場合は、自作しておくと使い勝手が良いです。
CSS
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 | li { list-style-type : none ; } #calendar { width : 350px ; border-top-width : 1px ; border-left-width : 1px ; border-top-style : solid ; border-left-style : solid ; border-top-color : #CCC ; border-left-color : #CCC ; } #day { clear : left ; } #cont { text-align : center ; padding : 5px ; width : 340px ; } .w { line-height : 30px ; text-align : center ; height : 30px ; width : 49px ; float : left ; border-right-width : 1px ; border-bottom-width : 1px ; border-right-style : solid ; border-bottom-style : solid ; border-right-color : #CCC ; border-bottom-color : #CCC ; background-color : #EFEFEF ; } .d { line-height : 30px ; text-align : center ; height : 30px ; width : 49px ; float : left ; border-right-width : 1px ; border-bottom-width : 1px ; border-right-style : solid ; border-bottom-style : solid ; border-right-color : #CCC ; border-bottom-color : #CCC ; } |
PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 | <?php /* * カレンダーを表示する */ function calender( $year , $month ) { $l_day = date ( "j" , mktime (0, 0, 0, $month + 1, 0, $year )); //月末を求める //前の月 if ( $month ==1){ $Before_Y = $year -1; $Before_M = 12; } else { $Before_Y = $year ; $Before_M = $month -1; } //次の月 if ( $month ==12){ $Later_Y = $year +1; $Later_M = 1; } else { $Later_Y = $year ; $Later_M = $month +1; } $tmp .= '<div id="cont">' ; $tmp .= sprintf( '<a href="calendar.php?y=%s&m=%s">←</a>' , $Before_Y , $Before_M ); $tmp .= sprintf( ' <strong>%s/%s</strong> ' , $year , $month );; $tmp .= sprintf( '<a href="calendar.php?y=%s&m=%s">→</a>' , $Later_Y , $Later_M ); $tmp .= '</div>' ; $tmp .= <<<EOM <div id= "calendar" > <div id= "week" > <div class = "w" >日</div> <div class = "w" >月</div> <div class = "w" >火</div> <div class = "w" >水</div> <div class = "w" >木</div> <div class = "w" >金</div> <div class = "w" >土</div> </div> <ul id= "day" > EOM; //$iが月末になるまで繰り返す for ( $i = 1; $i < $l_day + 1; $i ++) { $week = date ( "w" , mktime (0, 0, 0, $month , $i , $year )); $mk = mktime (0,0,0, $month , $i , $year ); $bgcolor = '' ; if ( $week == 0) $bgcolor = '#FFCCFF' ; //日曜日の背景色 if ( $week == 6) $bgcolor = '#CAE4FF' ; //土曜日の背景色 if ( $i == 1) $tmp .= str_repeat ( ' <li class="d" style=" background:#ddd"> </li>' . "\n" , $week ); $tmp .= ' <li class="d" style=" background:' . $bgcolor . '">' . $i . '</li> ' . "\n" ; if ( $i == $l_day ) $tmp .= str_repeat ( ' <li class="d" style=" background:#ddd"> </li>' . "\n" , 6 - $week ); } $tmp .= '</ul>' . "\n" ; $tmp .= '<br clear="left">' . "\n" ; $tmp .= '</div>' . "\n" ; return $tmp ; } //パラメーターが無い場合は今日の年と月にする if (! $_GET [ 'y' ]) $_GET [ 'y' ] = date ( 'Y' ); if (! $_GET [ 'm' ]) $_GET [ 'm' ] = date ( 'm' ); echo calender( $_GET [ 'y' ], $_GET [ 'm' ]); ?> |
実際は渡された日付が正しいかどうかなのどの確認が必要ですが、シンプルに書くとこんな感じです。日付に応じて例えば満室の場合は背景色を変えるなどの処理を入れたりなど。