楽せず楽しむ

仕事、生活、生き方、考え方、広い意味での自立を目指して頑張ります。

カレンダー用の配列を作成する

<?php
//リクエストが無ければ当月
$request_date = time();
if(isset($_GET['ym'])){
    //指定付があればその月(『Y-m-d』『Y-m』形式でも可)
    $request_date = strtotime($_GET['ym']);
}

$time_stamp = strtotime(date('Y-m-01', $request_date));
$end = strtotime(date('Y-m-t', $time_stamp));

$week = ['日', '月', '火', '水', '木', '金', '土'];
while($time_stamp <= $end){
    $dates[] = date('Y-m-d', $time_stamp);
    //keyを日付にして、中身に曜日を持たせる
    $dates_week[date('Y-m-d', $time_stamp)] = $week[date('w', $time_stamp)];
    $time_stamp = strtotime(date('Y-m-d', $time_stamp).'+1 day');
}

HTML側でカレンダー体裁を整える。