Instantly share code, notes, and snippets.

Embed
What would you like to do?
<?php
class Controller_Calendar extends Controller
{
public function action_index()
{
//現在のタイムスタンプ
$timestamp = time();
//現在の年月
$data['this_ym'] = date('Y年m月');
//現在月の日数を取得
$lday = date("t",$timestamp);
//現在月の1日は何曜日か。数字で取得:0 (日曜)から 6 (土曜)
$youbi = date("w", mktime(0,0,0,date("m",$timestamp),1,date("Y",$timestamp)));
//変数の初期化
$week = "";
//週の始まりが1日ではない場合、空のセルを作る
$week .= str_repeat('<td></td>', $youbi);
//日付の入ったセルを週毎に生成する
for($day = 1; $day <= $lday; $day++, $youbi++)
{
$week .= sprintf('<td class="youbi_%d">%d</td>', $youbi % 7, $day);
if($youbi % 7 == 6 OR $day == $lday)
{
if($day == $lday)
{
$week .= str_repeat('<td></td>', 6 - ($youbi % 7));
}
$weeks[] = '<tr>' . $week . '</tr>';
$week = "";
}
}
//データをviewへ渡すための配列へ入れる
$data['weeks'] = $weeks;
return View::forge("calendar/index",$data,false);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment