WordPress  
  予約システムを作る場合必須のカレンダーの作り方
2020
08
Aug

予約システムを作る場合必須のカレンダーの作り方

最終更新日: 2021年4月14日

ワードプレスには get_calendar という関数があります。get_calendar() と書くだけで出力されます。便利です。get_calendar については次に回して、今回はワードプレスを使わないサイト向けです。

2024年03月19日 今月のカレンダーは以下のようになります。後はデザインすれば済みです。
     12
3456789
10111213141516
17181920212223
24252627282930
31      

上記カレンダーは以下のコードで出力されています。


//当月の日付(一日から末日まで)
$cell = range(1, date('t'));

//月初と月末の曜日
$first_day = date('w', strtotime('first day of this month'));
$last_day = date('w', strtotime('last day of this month'));

//月初の余白を作成
if ($first_day > 0) {
    $cell = array_merge(array_fill(0, $first_day, ''), $cell);
}

//月末の余白を追加
if ($last_day < 6) {
    $cell = array_merge($cell, array_fill(0, 6 - $last_day , ''));
}

//カレンダーの行数に分ける
$cell = array_chunk($cell, 7);

//カレンダーを表示する
echo '<table>';

echo '<tr><th>日</th><th>月</th><th>火</th><th>水</th><th>木</th><th>金</th><th>土</th></tr>';

foreach ($cell as $week) {
    echo '<tr>';

    foreach ($week as $date) {
        if ($date) {
            echo '<td>' . $date . '</td>';
        } else {
    	    echo '<td>&nbsp;</td>';
        }
    }
    echo '</tr>';
}
echo '</table>';

 

get_calendarについての関連記事
予約システムを作るならget_calendar のカスタマイズがおすすめ

 

top