- 2009年1月18日 1:57 PM
- PHP
PEAR::Calendarパッケージをインストールしてカレンダーを表示してみる。
該当パッケージ(Calendar)を検索。
Macintosh:~ showjin$ pear search calendar
Retrieving data...0%Matched packages, channel pear.php.net:
=======================================
Package Stable/(Latest) Local
Calendar 0.5.4 (beta) A package for building Calendar data structures (irrespective of output)
beta版のようなので、これを指定していインストールする。
Macintosh:~ showjin$ sudo pear install -a Calendar-beta
downloading Calendar-0.5.4.tgz ...
Starting to download Calendar-0.5.4.tgz (54,759 bytes)
.............done: 54,759 bytes
downloading Date-1.4.7.tgz ...
Starting to download Date-1.4.7.tgz (55,754 bytes)
...done: 55,754 bytes
install ok: channel://pear.php.net/Calendar-0.5.4
install ok: channel://pear.php.net/Date-1.4.7
以下、PHPコード。
*これは2008年12月のカレンダーを指定しているが
$calMonth = new Calendar_Month_Weekdays(2008, 12, 0);
の部分を
$calMonth = new Calendar_Month_Weekdays(date("Y"), date("m"), 0);
こう書き換えれば、現在の月のカレンダーを表示できる。
<html>
<head>
<title>PEAR::Calendarでカレンダー表示</title>
</head>
<body>
<?php
// Calendarパッケージを読み込む
require_once("Calendar/Month/Weekdays.php");
// カレンダーを作成
// 2008, 12, 0 の部分は
// 年、月、最初の曜日(0なら日曜、2なら火曜、6なら土曜)
$calMonth = new Calendar_Month_Weekdays(2008, 12, 0);
// 現在の月のカレンダーを表示したい場合は以下のように記述する
// $calMonth = new Calendar_Month_Weekdays(date("Y"), date("m"), 0);
// カレンダーのオブジェクトを作成
$calMonth->build();
// $calMonth->thisYear()でセットされた年を取得
// $calMonth->thisMonth()でセットされた月を取得
print "<h2>".$calMonth->thisYear()."年".$calMonth->thisMonth()."月のカレンダー</h2>";
print "<table border='1'><tr>";
print "<th>日</th><th>月</th><th>火</th><th>水</th><th>木</th><th>金</th><th>土</th>";
print "</tr>";
// $calMonth->fetch()で、子オブジェクトを順に取得(ここでは「日」を取得)
while ($day = $calMonth->fetch()) {
// $day->isFirst() Dayオブジェクトが週の最初の日の場合trueを返す
if ($day->isFirst()) {
print "<tr>";
}
// $day->isEmpty() 曜日に対してDayオブジェクトが空の場合にtrueを返す
if ($day->isEmpty()) {
print "<td> </td>";
} else {
// $day->thisDay() セットされた日を取得
print "<td>".$day->thisDay()."</td>";
}
if ($day->isLast()) {
// $day->isLast() Dayオブジェクトが週の最後の日である場合にtrueを返す
print "</tr>";
}
}
print "</table>";
?>
</body>
</html>
- 新しい: XHMLの正しいマークアップ
- 古い: PEAR::PEAR_Info
コメント:0
トラックバック:0
- この記事のトラックバック URL
- http://showzine.info/blog/2009/01/pearcalendar.html/trackback
- トラックバックの送信元リスト
- PEAR::Calendar - SHOWJIN*BLOG より