ロゴ
HOME > PHPの便利な小技 > PHPでカレンダーを作る

PHPでカレンダーを作る

2014年03月07日

カレンダー用のライブラリーが色々と有りますので、表示するだけなら自作することもないのですが、プログラムに組み込んで色々とやりたいとき(予約システムのカレンダーなど)の場合は、自作しておくと使い勝手が良いです。

サンプル

CSS

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

<?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&amp;m=%s">←</a>' ,$Before_Y ,$Before_M );
       $tmp .=   sprintf(' &nbsp;<strong>%s/%s</strong>&nbsp; ' ,$year ,$month );;
       $tmp .=   sprintf('<a href="calendar.php?y=%s&amp;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">&nbsp;</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">&nbsp;</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']);

?>

実際は渡された日付が正しいかどうかなのどの確認が必要ですが、シンプルに書くとこんな感じです。日付に応じて例えば満室の場合は背景色を変えるなどの処理を入れたりなど。