サンプル・プログラムの実行例

サンプル・プログラム
tripleCalendar.php | サンプル・プログラム本体。 |
pahooCalendar.php | 暦計算クラス pahooCalendar。 暦計算クラスの使い方は「PHPで日出没・月出没・月齢・潮を計算」を参照。include_path が通ったディレクトリに配置すること。 |
解説:旧暦の計算
1407: /**
1408: * グレゴオリオ暦=旧暦テーブル 作成
1409: * @param int $year 西暦年
1410: * @return double 太陽の黄経(視黄経)
1411: */
1412: function makeLunarCalendar($year) {
1413: //旧暦の2033年問題により、2033年以降はエラーフラグを立てる
1414: if ($year >= 2033) {
1415: $this->error = TRUE;
1416: $this->errmsg = '2033年以降は正しい旧暦計算ができません';
1417: }
1418:
1419: unset($this->tblmoon);
1420: $this->tblmoon = array();
1421:
1422: //前年の冬至を求める
1423: for ($day = 1; $day <= 31; $day++) {
1424: $lsun = $this->longitude_sun($year - 1, 12, $day, 0, 0, 0);
1425: if (floor($lsun / 15.0) > 17) break;
1426: }
1427: $d1 = $day - 1; //冬至
1428:
1429: //翌年の雨水を求める
1430: for ($day = 1; $day <= 31; $day++) {
1431: $lsun = $this->longitude_sun($year + 1, 2, $day, 0, 0, 0);
1432: if (floor($lsun / 15.0) > 22) break;
1433: }
1434: $d2 = $day - 1; //雨水
1435:
1436: //朔の日を求める
1437: $cnt = 0;
1438: $dd = $d1;
1439: $mm = 12;
1440: $yy = $year - 1;
1441: while ($yy <= $year + 1) {
1442: $dm = $this->getDaysInMonth($yy, $mm);
1443: while ($dd <= $dm) {
1444: $age1 = $this->moon_age($yy, $mm, $dd, 0 - $this->TDIFF, 0, 0); //Ver.3.11 bug-fix
1445: $age2 = $this->moon_age($yy, $mm, $dd, 23 - $this->TDIFF, 59, 59); //Ver.3.11 bug-fix
1446: if ($age2 <= $age1) {
1447: $this->tblmoon[$cnt]['year'] = $yy;
1448: $this->tblmoon[$cnt]['month'] = $mm;
1449: $this->tblmoon[$cnt]['day'] = $dd;
1450: $this->tblmoon[$cnt]['age'] = $age1;
1451: $this->tblmoon[$cnt]['jd'] = $this->Gregorian2JD($yy, $mm, $dd, 0, 0, 0);
1452: $cnt++;
1453: }
1454: $dd++;
1455: }
1456: $mm++;
1457: $dd = 1;
1458: if ($mm > 12) {
1459: $yy++;
1460: $mm = 1;
1461: }
1462: }
1463:
1464: //二十四節気(中)を求める
1465: $tblsun = array();
1466: $cnt = 0;
1467: $dd = $d1;
1468: $mm = 12;
1469: $yy = $year - 1;
1470: while ($yy <= $year + 1) {
1471: $dm = $this->getDaysInMonth($yy, $mm);
1472: while ($dd <= $dm) {
1473: $l1 = $this->longitude_sun($yy, $mm, $dd, 0, 0, 0);
1474: $l2 = $this->longitude_sun($yy, $mm, $dd, 24, 0, 0);
1475: $n1 = floor($l1 / 15.0);
1476: $n2 = floor($l2 / 15.0);
1477: if (($n2 != $n1) && ($n2 % 2 == 0)) {
1478: $tblsun[$cnt]['jd'] = $this->Gregorian2JD($yy, $mm, $dd, 0, 0, 0);
1479: $oldmonth = floor($n2 / 2) + 2;
1480: if ($oldmonth > 12) $oldmonth -= 12;
1481: $tblsun[$cnt]['oldmonth'] = $oldmonth;
1482: $cnt++;
1483: }
1484: $dd++;
1485: }
1486: $mm++;
1487: $dd = 1;
1488: if ($mm > 12) {
1489: $yy++;
1490: $mm = 1;
1491: }
1492: }
1493:
1494: //月の名前を決める
1495: $n1 = count($this->tblmoon);
1496: $n2 = count($tblsun);
1497: for ($i = 0; $i < $n1 - 1; $i++) {
1498: for ($j = 0; $j < $n2; $j++) {
1499: if (($this->tblmoon[$i]['jd'] <= $tblsun[$j]['jd'])
1500: && ($this->tblmoon[$i + 1]['jd'] > $tblsun[$j]['jd'])) {
1501: $this->tblmoon[$i]['oldmonth'] = $tblsun[$j]['oldmonth'];
1502: $this->tblmoon[$i]['oldleap'] = FALSE;
1503: $this->tblmoon[$i + 1]['oldmonth'] = $tblsun[$j]['oldmonth'];
1504: $this->tblmoon[$i + 1]['oldleap'] = TRUE;
1505: break;
1506: }
1507: }
1508: }
1509: }

変換のための方程式を導出できないため、あらかじめ計算したい西暦年から変換テーブルを作る必要がある。これを行うのがユーザー関数 makeLunarCalendar である。
- 前年の冬至を求める。
- 翌年の雨水を求める。
- 1~2 の期間中の二十四節気(中)を配列 $tblsun に格納する。
- 朔日と中を比較して、月の名前と閏月を決めてゆく。
1511: /**
1512: * 旧暦を求める
1513: * @param int $year 西暦年
1514: * @param int $month 月
1515: * @param int $day 日
1516: * @return array(旧暦月,日,閏月フラグ)/FALSE:旧暦計算不能
1517: */
1518: function Gregorian2Lunar($year, $month, $day) {
1519: //2033年問題チェック
1520: if ($this->error) return FALSE;
1521:
1522: $jd = $this->Gregorian2JD($year, $month, $day, 0, 0, 0);
1523: $str = '';
1524: $n1 = count($this->tblmoon);
1525: for ($i = 0; $i < $n1 - 1; $i++) {
1526: if ($jd < $this->tblmoon[$i + 1]['jd']) {
1527: $day = floor($jd - $this->tblmoon[$i]['jd']) + 1;
1528: $items = array($this->tblmoon[$i]['oldmonth'], $day, $this->tblmoon[$i]['oldleap']);
1529: break;
1530: }
1531: }
1532: return $items;
1533: }

この旧暦計算法は天保暦に基づいているが、天保暦には 2033 年問題がある。西暦 2033 年(令和 15 年)10 月は旧暦 9 月だが、11 月に入ると旧暦 11 月と、10 月が無くなってしまう問題である。
これは、1844 年(天保 14 年)に天保暦が導入されてから初めて起きる事態だが、天保暦は 1872 年(明治 5 年)に廃止されているために正式な解決法は用意されていない。
そこで、2033 年(令和 15 年)以降の旧暦計算はスキップするようにした。
解説:干支と十二支の計算
1554: /**
1555: * 干支を求める(下請け関数)
1556: * @param int $a1 十干の基準値
1557: * @param int $a2 十二支の基準値
1558: * @param int $n 計算したい値
1559: * @return string 干支
1560: */
1561: function __eto($a1, $a2, $n) {
1562: //十干
1563: static $table1 = array(
1564: 0 =>'甲',
1565: 1 =>'乙',
1566: 2 =>'丙',
1567: 3 =>'丁',
1568: 4 =>'戊',
1569: 5 =>'己',
1570: 6 =>'庚',
1571: 7 =>'辛',
1572: 8 =>'壬',
1573: 9 =>'癸'
1574: );
1575:
1576: //十二支
1577: static $table2 = array(
1578: 0 =>'子',
1579: 1 =>'丑',
1580: 2 =>'寅',
1581: 3 =>'卯',
1582: 4 =>'辰',
1583: 5 =>'巳',
1584: 6 =>'午',
1585: 7 =>'未',
1586: 8 =>'申',
1587: 9 =>'酉',
1588: 10 =>'戌',
1589: 11 =>'亥'
1590: );
1591:
1592: return $table1[abs($n - $a1) % 10] . $table2[abs($n - $a2) % 12];
1593: }
干支は、甲・乙・丙・丁・戊・己・庚・辛・壬・癸の 10要素からなる十干と、子・丑・寅・卯・辰・巳・午・未・申・酉・戌・亥の 12要素からなる十二支の組み合わせからなる。
つまり、十干は年/月/日が 10 回ごとに繰り返され、十二支は年/月/日が 12 回ごとに繰り返されることになる。これを計算するのが __eto である。

次に、基準となる年月日を調べておく。
甲の年は西暦 1904 年(明治 37 年)、子の年は西暦 1900 年(明治 33 年)である。年の干支は、この 2 つを基準に計算する。
甲子の月は 1903 年(明治 36 年)11 月である。月の干支は、ここを基準に計算する。
甲子の日は 1902 年(明治 35 年)4 月 11 日である。日の干支は、ここを基準に計算する。

ちなみに、干支も十二支も古代中国で考え出されたものだが、十二支の方は、木星が黄道を 1 周するのがほぼ 12 年(公転周期 11.86 年)ということに対応している。古代中国では木星を暦に関わる重要な天体と位置づけており、歳星という名で呼ばれていた。
解説:カレンダー作成
0174: /**
0175: * 1ヶ月分のカレンダーを作成
0176: * @param object $pcl pahooCalendarオブジェクト
0177: * @param int $start 週の開始曜日(0:日曜日, 1:月曜日...6:土曜日)
0178: * @param int $year 西暦年
0179: * @param int $month 月
0180: * @return string HTMLコンテンツ/FALSE:エラー
0181: */
0182: function makeCalendar($pcl, $start, $year, $month) {
0183: if ($month < 1 && $month > 12) return FALSE;
0184:
0185: $eto_year = $pcl->eto_year($year);
0186: $eto_month = $pcl->eto_month($year, $month);
0187:
0188: $html =<<< EOT
0189: <table class="calendar">
0190: <tr>
0191: <th colspan="7"><span class="large">{$year}年({$eto_year}) {$month}月({$eto_month})</span></th>
0192: </tr>
0193: <tr>
0194:
0195: EOT;
0196:
0197: //曜日の行
0198: for ($i = 0; $i < 7; $i++) {
0199: $n = ($start + $i) % 7;
0200: if ($n == 6) $class = 'blue';
0201: else if ($n == 0) $class = 'red';
0202: else $class = 'black';
0203: $str = $pcl->__getWeekString($n);
0204: $html .= "<th><span class=\"{$class}\">{$str}</span></th>";
0205: }
0206: $html .= "</tr>\n";
0207:
0208: //カレンダー本体
0209: $wn1 = $pcl->getWeekNumber($year, $month, 1); //月の最初の曜日
0210: $dim = $pcl->getDaysInMonth($year, $month); //月の最後の日
0211: $cnt = 0;
0212: $flag = FALSE;
0213: $n = $start;
0214: $day = 1;
0215: while (1) {
0216: if ($cnt % 7 == 0) $html .= "<tr>\n";
0217: //曜日の色
0218: if ($n % 7 == 6) $class = 'blue';
0219: else if ($n % 7 == 0) $class = 'red';
0220: else $class = 'black';
0221: if ($n % 7 == $wn1) $flag = TRUE;
0222: //表示開始
0223: if ($flag) {
0224: //祝日
0225: $holiday = $pcl->getHoliday($year, $month, $day);
0226: if ($holiday != '') $class = 'red';
0227: $holiday = ($holiday == FALSE) ? '<br />' :
0228: "<span class=\"small red\">{$holiday}</span><br />";
0229: //二十四節気
0230: $solarterm = $pcl->getSolarTerm($year, $month, $day);
0231: $solarterm = ($solarterm == '') ? '<br />' :
0232: "<span class=\"small\">{$solarterm}</span><br />";
0233: //七十二候
0234: $solarterm72 = $pcl->getSolarTerm72($year, $month, $day);
0235: $solarterm72 = ($solarterm72 == '') ? '<br />' :
0236: "<span class=\"small\">{$solarterm72}</span><br />";
0237: //旧暦
0238: list($oldmonth, $oldday, $oldleap) = $pcl->Gregorian2Lunar($year, $month, $day);
0239: if (!$pcl->error) {
0240: $oldleap = $oldleap ? '閏' : '';
0241: $rokuyou = $pcl->rokuyou($oldmonth, $oldday);
0242: $oldcal = sprintf('%s%d月%d日<br />(%s)', $oldleap, $oldmonth, $oldday, $rokuyou);
0243: //2033年問題回避
0244: } else {
0245: $oldcal = '';
0246: }
0247: //日の干支
0248: $eto_day = $pcl->eto_day($year, $month, $day);
0249: //表示
0250: $html .=<<< EOT
0251: <td>
0252: <span class="large {$class}">{$day}</span><br />
0253: {$holiday}
0254: {$solarterm}
0255: {$solarterm72}
0256: <span class="small">{$eto_day}<br /></span>
0257: <span class="small">{$oldcal}</span>
0258: </td>
0259:
0260: EOT;
0261: $day++;
0262: if ($day > $dim) break;
0263: } else {
0264: $html .= "<td> </td>";
0265: }
0266: $cnt++;
0267: $n++;
0268: if ($cnt % 7 == 0) $html .= "</tr>\n";
0269: }
0270: //最後の日以降の処理
0271: $cnt++;
0272: while ($cnt % 7 != 0) {
0273: $html .= "<td> </td>";
0274: $cnt++;
0275: if ($cnt % 7 == 0) $html .= "</tr>\n";
0276: }
0277:
0278: $html .= "</table>\n";
0279:
0280: return $html;
0281: }
週の開始曜日を自由に設定できるようにするための工夫をしている。

カレンダー本体を作成する処理では、まず、月の最初の曜日 $wn1 と、月の最後の日 $dim を計算しておく。
$wn1 と週の開始曜日 $start が一致するまでは変数 $flag が FALSE のままで、この時は日付を入れずに空のままにしておく。
$flag が TRUE になったら日付を入れてゆく。あわせて、二十四節気、旧暦の計算を行う。
$dim に達したらループを抜け出し、行末まで空白のセルを追加してゆく。
質疑応答
「PHP で 3 ヶ月カレンダーを作る」のページのソースなのですが「サンプル・プログラムの解説:カレンダー作成」のソースの 0171行目に「<tr>」の開始タグを入れたほうがいいと思います。曜日部分の行部分 TR の開始タグが無いようです。
間違えであればすいません。
【回答】
ご指摘ありがとうございます。<tr> タグが抜けていました。追加しました。
活用例
参考サイト
- PHP で祝日を求める:ぱふぅ家のホームページ
- PHP で二十四節気一覧を作成:ぱふぅ家のホームページ
- 旧暦と六曜を作りましょう:こよみのページ
- 七十二候:今日は何の日~毎日が記念日~
- 旧暦 2033 年問題:Oka Laboratory 備忘録
- 旧暦六曜 Java ソースプログラム公開:がらくた研究室
- 「月の名前」の計算方法:ここですという名前のブログやさん
「PHP で祝日を求める」「PHP で二十四節気一覧を作成」に、七十二候や旧暦、六曜、干支の計算を加えた。
(2019 年 6 月 30 日)makeCalendar の不具合を修正、リファラチェックを追加した。
(2019 年 12 月 20 日)2020 年(令和 2 年)の祝日変更、旧暦の 2033 年(令和 15 年)問題に対応した。