Skip to content

Instantly share code, notes, and snippets.

@s-hiroshi
Created May 14, 2012 06:18
Show Gist options
  • Save s-hiroshi/2692130 to your computer and use it in GitHub Desktop.
Save s-hiroshi/2692130 to your computer and use it in GitHub Desktop.
WordPress > draw calender
/*
* カレンダー出力関数
* @param $id 投稿ID
* @param $y 年 数字4桁
* @param $m 月 数字1~13
* @param $days 日付 配列 添え字[1~31] 値[空, close, holiday]
*/
function drawCalender ($id, $y, $m, $days) {
// $y 年 $m 月
$t = mktime(0, 0, 0, $m, 1, $y); //$y年$m月1日のUNIXTIME
$w = date('w', $t); //1日の曜日(0:日~6:土)
$n = date('t', $t); //$y年$m月の日数
$F = date('F', $t); // $mの英語(FebruaryからDecember)
if ($m<10) {
$m = "0" . $m;
}
print <<<HTML
<table>
<caption>{$y} {$F}</caption>
<tr>
<th class="textRed">Sun</th>
<th>Mon</th>
<th>Tue</th>
<th>Wed</th>
<th>Thu</th>
<th>Fri</th>
<th>Sat</th>
</tr>
HTML;
for($i=1-$w; $i<= $n+7; $i++){
if ((($i + $w) % 7) == 1) {
print "<tr>\n";
}
// 日付が有効な場合の処理
if ((0 < $i) && ($i <= $n)) {
//$key = "day" . $i;
$value = $days[$i];
$html = "<td";
// 曜日の取得
$hizuke = mktime(0, 0, 0, $m, $i, $y); //$y年$m月$i日のUNIXTIME
$youbi = date('w', $hizuke); //1日の曜日(0:日~6:土)
// 定休日
if ($value == 'close') { // 定休日
$html .= ' class="close"';
} else if ($value == 'holiday') { // 祝日
$html .= ' class="holiday"';
} else if ($youbi == 0) { // 日曜
$html .= ' class="sun"';
}
$html .= '>' . $i . '</td>';
print($html);
} else {
print "<td>&nbsp;</td>\n";
}
if ((($i + $w ) % 7 ) == 0 ) {
print "</tr>\n";
if ( $i >= $n ) {
break;
}
}
}
print "</table>\n";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment