.gitignore000064400000000035146412202240006527 0ustar00.idea/ vendor/ composer.lock LICENSE000064400000002045146412202240005547 0ustar00MIT License Copyright (c) 2018 6tail Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.README.md000064400000002727146412202240006030 0ustar00# lunar [![License](https://img.shields.io/badge/license-MIT-4EB1BA.svg?style=flat-square)](https://github.com/6tail/lunar-php/blob/master/LICENSE) lunar是一款无第三方依赖的公历(阳历)和农历(阴历、老黄历)工具,支持星座、儒略日、干支、生肖、节气、节日、彭祖百忌、每日宜忌、吉神宜趋、凶煞宜忌、吉神(喜神/福神/财神/阳贵神/阴贵神)方位、胎神方位、冲煞、纳音、星宿、八字、五行、十神、建除十二值星、青龙名堂等十二神、黄道黑道日及吉凶等。 > 基于php5.3开发。 [English](https://github.com/6tail/lunar-php/blob/master/README_EN.md) ## 示例 composer require 6tail/lunar-php toFullString()."\n"; echo $lunar->getSolar()->toFullString()."\n"; 输出结果: 一九八六年四月廿一 丙寅(虎)年 癸巳(蛇)月 癸酉(鸡)日 子(鼠)时 纳音[炉中火 长流水 剑锋金 桑柘木] 星期四 北方玄武 星宿[斗木獬](吉) 彭祖百忌[癸不词讼理弱敌强 酉不会客醉坐颠狂] 喜神方位[巽](东南) 阳贵神方位[巽](东南) 阴贵神方位[震](正东) 福神方位[兑](正西) 财神方位[离](正南) 冲[(丁卯)兔] 煞[东] 1986-05-29 00:00:00 星期四 双子座 ## 文档 请移步至 [http://6tail.cn/calendar/api.html](http://6tail.cn/calendar/api.html "http://6tail.cn/calendar/api.html") README_EN.md000064400000002132146412202240006400 0ustar00# lunar [![License](https://img.shields.io/badge/license-MIT-4EB1BA.svg?style=flat-square)](https://github.com/6tail/lunar-php/blob/master/LICENSE) lunar is a calendar library for Solar and Chinese Lunar. [简体中文](https://github.com/6tail/lunar-php/blob/master/README.md) ## Example composer require 6tail/lunar-php toFullString()."\n"; echo $lunar->getSolar()->toFullString()."\n"; Output: 一九八六年四月廿一 丙寅(虎)年 癸巳(蛇)月 癸酉(鸡)日 子(鼠)时 纳音[炉中火 长流水 剑锋金 桑柘木] 星期四 北方玄武 星宿[斗木獬](吉) 彭祖百忌[癸不词讼理弱敌强 酉不会客醉坐颠狂] 喜神方位[巽](东南) 阳贵神方位[巽](东南) 阴贵神方位[震](正东) 福神方位[兑](正西) 财神方位[离](正南) 冲[(丁卯)兔] 煞[东] 1986-05-29 00:00:00 星期四 双子座 ## Documentation Please visit [http://6tail.cn/calendar/api.html](http://6tail.cn/calendar/api.html "http://6tail.cn/calendar/api.html") composer.json000064400000002301146412202240007257 0ustar00{ "name": "6tail/lunar-php", "type": "library", "license": "MIT", "authors": [ { "name": "6tail", "email": "6tail@6tail.cn", "homepage": "http://6tail.cn" } ], "minimum-stability": "stable", "require": { "php": ">=5.3", "ext-bcmath": "*" }, "autoload": { "psr-4": { "com\\nlf\\calendar\\": "src/" } }, "homepage": "https://github.com/6tail/lunar-php", "keywords": [ "calendar", "lunar", "solar", "节假日", "星座", "儒略日", "干支", "生肖", "节气", "节日", "彭祖百忌", "每日宜忌", "时辰宜忌", "吉神宜趋", "凶煞宜忌", "喜神", "福神", "财神", "阳贵神", "阴贵神", "胎神", "冲煞", "纳音", "星宿", "八字", "五行", "十神", "建除十二值星", "青龙名堂等十二神", "黄道黑道日" ], "description": "lunar is a calendar library for Solar and Chinese Lunar." } demo.php000064400000020205146412202240006175 0ustar00toFullString() . "\n"; echo $lunar->getSolar()->toFullString() . "\n"; // 节假日 echo HolidayUtil::getHoliday('2020-05-02') . "\n"; // 儒略日 $solar = Solar::fromYmd(2020, 7, 15); echo $solar->getJulianDay() . "\n"; $solar = Solar::fromJulianDay(2459045.5); echo $solar->toFullString() . "\n"; // 遍历节气表 $lunar = Lunar::fromDate(new DateTime()); $jieQi = $lunar->getJieQiTable(); foreach ($jieQi as $key => $value) { echo $key . ' = ' . $value->toYmdHms() . "\n"; } // 遍历日吉神(宜趋) foreach ($lunar->getDayJiShen() as $js) { echo $js . ' '; } echo "\n"; // 遍历时辰宜 foreach ($lunar->getTimeYi() as $yi) { echo $yi . ' '; } echo "\n"; // 八字 $baZi = $lunar->getEightChar(); echo $baZi . ' '; echo "\n"; // 八字五行 echo $baZi->getYearWuXing() . ' ' . $baZi->getMonthWuXing() . ' ' . $baZi->getDayWuXing() . ' ' . $baZi->getTimeWuXing(); echo "\n"; // 八字纳音 echo $baZi->getYearNaYin() . ' ' . $baZi->getMonthNaYin() . ' ' . $baZi->getDayNaYin() . ' ' . $baZi->getTimeNaYin(); echo "\n"; // 八字天干十神 echo $baZi->getYearShiShenGan() . ' ' . $baZi->getMonthShiShenGan() . ' ' . $baZi->getDayShiShenGan() . ' ' . $baZi->getTimeShiShenGan(); echo "\n"; // 遍历八字年支十神 foreach ($baZi->getYearShiShenZhi() as $shen) { echo $shen . ' '; } echo "\n"; // 遍历八字月支十神 foreach ($baZi->getMonthShiShenZhi() as $shen) { echo $shen . ' '; } echo "\n"; // 遍历八字日支十神 foreach ($baZi->getDayShiShenZhi() as $shen) { echo $shen . ' '; } echo "\n"; // 遍历八字时支十神 foreach ($baZi->getTimeShiShenZhi() as $shen) { echo $shen . ' '; } echo "\n"; // 八字胎元 echo $baZi->getTaiYuan(); echo "\n"; // 八字命宫 echo $baZi->getMingGong(); echo "\n"; // 八字身宫 echo $baZi->getShenGong(); echo "\n"; // 时辰吉神方位 echo $lunar->getTimePositionFu() . "\n"; echo $lunar->getTimePositionXi() . "\n"; echo $lunar->getTimePositionCai() . "\n"; echo $lunar->getTimePositionYinGui() . "\n"; echo $lunar->getTimePositionYangGui() . "\n"; echo $lunar->getTimePositionFuDesc() . "\n"; echo $lunar->getTimePositionXiDesc() . "\n"; echo $lunar->getTimePositionCaiDesc() . "\n"; echo $lunar->getTimePositionYinGuiDesc() . "\n"; echo $lunar->getTimePositionYangGuiDesc() . "\n"; // 指定阳历时间得到八字信息 $solar = Solar::fromYmdHms(1988, 2, 15, 23, 0, 0); $lunar = $solar->getLunar(); $baZi = $lunar->getEightChar(); echo $baZi->getYear() . ' ' . $baZi->getMonth() . ' ' . $baZi->getDay() . ' ' . $baZi->getTime() . "\n"; $solar = Solar::fromYmdHms(1988, 3, 20, 18, 0, 0); $lunar = $solar->getLunar(); $baZi = $lunar->getEightChar(); // 男运 $yun = $baZi->getYun(1); echo "\n"; echo '阳历' . $solar->toYmdHms() . '出生\n'; echo '出生' . $yun->getStartYear() . '年' . $yun->getStartMonth() . '个月' . $yun->getStartDay() . '天后起运' . "\n"; echo '阳历' . $yun->getStartSolar()->toYmd() . '后起运' . "\n"; echo "\n"; // 大运 $daYunArr = $yun->getDaYun(); for ($i = 0; $i < count($daYunArr); $i++) { $daYun = $daYunArr[$i]; echo '大运[' . $daYun->getIndex() . '] = ' . $daYun->getStartYear() . '年 ' . $daYun->getStartAge() . '岁 ' . $daYun->getGanZhi() . "\n"; } echo "\n"; // 第1次大运流年 $liuNianArr = $daYunArr[1]->getLiuNian(); for ($i = 0; $i < count($liuNianArr); $i++) { $liuNian = $liuNianArr[$i]; echo '流年[' . $liuNian->getIndex() . '] = ' . $liuNian->getYear() . '年 ' . $liuNian->getAge() . '岁 ' . $liuNian->getGanZhi() . "\n"; } echo "\n"; // 第1次大运小运 $xiaoYunArr = $daYunArr[1]->getXiaoYun(); for ($i = 0; $i < count($xiaoYunArr); $i++) { $xiaoYun = $xiaoYunArr[$i]; echo '小运[' . $xiaoYun->getIndex() . '] = ' . $xiaoYun->getYear() . '年 ' . $xiaoYun->getAge() . '岁 ' . $xiaoYun->getGanZhi() . "\n"; } echo "\n"; // 第1次大运首个流年的流月 $liuYueArr = $liuNianArr[0]->getLiuYue(); for ($i = 0; $i < count($liuYueArr); $i++) { $liuYue = $liuYueArr[$i]; echo '流月[' . $liuYue->getIndex() . '] = ' . $liuYue->getMonthInChinese() . '月 ' . $liuYue->getGanZhi() . "\n"; } echo "\n"; // 八字转阳历 try { $l = Solar::fromBaZi('庚子', '癸未', '乙丑', '丁亥'); foreach ($l as $d) { echo $d->toFullString() . "\n"; } } catch (Exception $e) { echo $e->getMessage(); } // 阳历日期推移 $date = Solar::fromYmd(2020, 1, 23); echo strcmp('2020-01-24', $date->next(1)->toYmd()); // 仅工作日,跨越春节假期 echo strcmp('2020-02-03', $date->nextWorkday(1)->toYmd()); $date = Solar::fromYmd(2020, 2, 3); echo strcmp('2020-01-31', $date->next(-3)->toYmd()); // 仅工作日,跨越春节假期 echo strcmp('2020-01-21', $date->nextWorkday(-3)->toYmd()); $date = Solar::fromYmd(2020, 2, 9); echo strcmp('2020-02-15', $date->next(6)->toYmd()); // 仅工作日,跨越周末 echo strcmp('2020-02-17', $date->nextWorkday(6)->toYmd()); $date = Solar::fromYmd(2020, 1, 17); echo strcmp('2020-01-18', $date->next(1)->toYmd()); // 仅工作日,周日调休按上班算 echo strcmp('2020-01-19', $date->nextWorkday(1)->toYmd()) . "\n"; // 节假日数据修改 echo strcmp('2020-01-01 元旦节 2020-01-01', HolidayUtil::getHoliday('2020-01-01') . ''); // 将2020-01-01修改为春节 HolidayUtil::fix(null, '202001011120200101'); echo strcmp('2020-01-01 春节 2020-01-01', HolidayUtil::getHoliday('2020-01-01') . ''); // 追加2099-01-01为元旦节 HolidayUtil::fix(null, '209901010120990101'); echo strcmp('2099-01-01 元旦节 2099-01-01', HolidayUtil::getHoliday('2099-01-01') . ''); // 将2020-01-01修改为春节,并追加2099-01-01为元旦节 HolidayUtil::fix(null, '202001011120200101209901010120990101'); echo strcmp('2020-01-01 春节 2020-01-01', HolidayUtil::getHoliday('2020-01-01') . ''); echo strcmp('2099-01-01 元旦节 2099-01-01', HolidayUtil::getHoliday('2099-01-01') . ''); // 更改节假日名称 $names = HolidayUtil::$NAMES; $names[0] = '元旦'; $names[1] = '大年初一'; HolidayUtil::fix($names, null); echo strcmp('2020-01-01 大年初一 2020-01-01', HolidayUtil::getHoliday('2020-01-01') . ''); echo strcmp('2099-01-01 元旦 2099-01-01', HolidayUtil::getHoliday('2099-01-01') . ''); // 追加节假日名称和数据 $names = array(); for ($i = 0, $j = count(HolidayUtil::$NAMES); $i < $j; $i++) { $names[$i] = HolidayUtil::$NAMES[$i]; } $names[9] = '我的生日'; $names[10] = '结婚纪念日'; $names[11] = '她的生日'; HolidayUtil::fix($names, '20210529912021052920211111:12021111120211201;120211201'); echo strcmp('2021-05-29 我的生日 2021-05-29', HolidayUtil::getHoliday('2021-05-29') . ''); echo strcmp('2021-11-11 结婚纪念日 2021-11-11', HolidayUtil::getHoliday('2021-11-11') . ''); echo strcmp('2021-12-01 她的生日 2021-12-01', HolidayUtil::getHoliday('2021-12-01') . '') . "\n"; // 节日 $solar = Solar::fromYmd(2020, 11, 26); foreach ($solar->getFestivals() as $f) { echo $f . "\n"; } $solar = Solar::fromYmd(2020, 6, 21); foreach ($solar->getFestivals() as $f) { echo $f . "\n"; } $solar = Solar::fromYmd(2021, 5, 9); foreach ($solar->getFestivals() as $f) { echo $f . "\n"; } $solar = Solar::fromYmd(1986, 11, 27); foreach ($solar->getFestivals() as $f) { echo $f . "\n"; } $solar = Solar::fromYmd(1985, 6, 16); foreach ($solar->getFestivals() as $f) { echo $f . "\n"; } $solar = Solar::fromYmd(1984, 5, 13); foreach ($solar->getFestivals() as $f) { echo $f . "\n"; } // 旬 $solar = Solar::fromYmdHms(2020, 11, 19, 0, 0, 0); $lunar = $solar->getLunar(); // 甲午 echo $lunar->getYearXun() . "\n"; // 旬空(空亡) // 辰巳 echo $lunar->getYearXunKong() . "\n"; // 午未 echo $lunar->getMonthXunKong() . "\n"; // 戌亥 echo $lunar->getDayXunKong() . "\n"; // 八字日柱旬空(空亡) $solar = Solar::fromYmdHms(1990, 12, 23, 8, 37, 0); $lunar = $solar->getLunar(); $eightChar = $lunar->getEightChar(); // 子丑 echo $eightChar->getDayXunKong() . "\n"; // 数九 $lunar = Lunar::fromDate(new DateTime()); echo $lunar->getShuJiu() . "\n"; // 三伏 $solar = Solar::fromYmd(2020, 7, 26); $lunar = $solar->getLunar(); echo $lunar->getFu()->toFullString() . "\n"; src/DaYun.php000064400000006770146412202240007073 0ustar00yun = $yun; $this->lunar = $yun->getLunar(); $this->index = $index; $birthYear = $this->lunar->getSolar()->getYear(); $year = $yun->getStartSolar()->getYear(); if ($index < 1) { $this->startYear = $birthYear; $this->startAge = 1; $this->endYear = $year - 1; $this->endAge = $year - $birthYear; } else { $add = ($index - 1) * 10; $this->startYear = $year + $add; $this->startAge = $this->startYear - $birthYear + 1; $this->endYear = $this->startYear + 9; $this->endAge = $this->startAge + 9; } } /** * 获取起始年 * @return int */ public function getStartYear() { return $this->startYear; } /** * 获取结束年 * @return int */ public function getEndYear() { return $this->endYear; } /** * 获取开始年龄 * @return int */ public function getStartAge() { return $this->startAge; } /** * 获取结束年龄 * @return int */ public function getEndAge() { return $this->endAge; } /** * 获取序数 * @return int */ public function getIndex() { return $this->index; } /** * 获取运 * @return Yun */ public function getYun() { return $this->yun; } /** * 获取阴历 * @return Lunar */ public function getLunar() { return $this->lunar; } /** * 获取干支 * @return string */ public function getGanZhi() { if ($this->index < 1) { return ''; } $offset = LunarUtil::getJiaZiIndex($this->lunar->getMonthInGanZhiExact()); $offset += $this->yun->isForward() ? $this->index : -$this->index; $size = count(LunarUtil::$JIA_ZI); if ($offset >= $size) { $offset -= $size; } if ($offset < 0) { $offset += $size; } return LunarUtil::$JIA_ZI[$offset]; } /** * 获取所在旬 * @return string 旬 */ public function getXun() { return LunarUtil::getXun($this->getGanZhi()); } /** * 获取旬空(空亡) * @return string 旬空(空亡) */ public function getXunKong() { return LunarUtil::getXunKong($this->getGanZhi()); } /** * 获取流年 * @return LiuNian[] */ public function getLiuNian() { $n = 10; if ($this->index < 1) { $n = $this->endYear - $this->startYear + 1; } $l = array(); for ($i = 0; $i < $n; $i++) { $l[] = new LiuNian($this, $i); } return $l; } /** * 获取小运 * @return XiaoYun[] */ public function getXiaoYun() { $n = 10; if ($this->index < 1) { $n = $this->endYear - $this->startYear + 1; } $l = array(); for ($i = 0; $i < $n; $i++) { $l[] = new XiaoYun($this, $i, $this->yun->isForward()); } return $l; } } src/EightChar.php000064400000032614146412202240007705 0ustar00 1, '丙' => 10, '戊' => 10, '庚' => 7, '壬' => 4, '乙' => 6, '丁' => 9, '己' => 9, '辛' => 0, '癸' => 3 ); /** * 月支,按正月起寅排列 * @var array */ public static $MONTH_ZHI = array('', '寅', '卯', '辰', '巳', '午', '未', '申', '酉', '戌', '亥', '子', '丑'); /** * 长生十二神 * @var array */ public static $CHANG_SHENG = array('长生', '沐浴', '冠带', '临官', '帝旺', '衰', '病', '死', '墓', '绝', '胎', '养'); function __construct($lunar) { $this->lunar = $lunar; } public static function fromLunar($lunar) { return new EightChar($lunar); } public function toString() { return $this->getYear() . ' ' . $this->getMonth() . ' ' . $this->getDay() . ' ' . $this->getTime(); } public function __toString() { return $this->toString(); } /** * 获取流派 * @return int 流派,2晚子时日柱按当天,1晚子时日柱按明天 */ public function getSect() { return $this->sect; } /** * 设置流派 * @param int $sect 流派,2晚子时日柱按当天,1晚子时日柱按明天 */ public function setSect($sect) { $this->sect = (1 == $sect) ? 1 : 2; } /** * 获取阴历对象 * @return Lunar 阴历对象 */ public function getLunar() { return $this->lunar; } /** * 获取年柱 * @return string 年柱 */ public function getYear() { return $this->lunar->getYearInGanZhiExact(); } /** * 获取年干 * @return string 天干 */ public function getYearGan() { return $this->lunar->getYearGanExact(); } /** * 获取年支 * @return string 地支 */ public function getYearZhi() { return $this->lunar->getYearZhiExact(); } /** * 获取年柱地支藏干,由于藏干分主气、余气、杂气,所以返回结果可能为1到3个元素 * @return string 天干 */ public function getYearHideGan() { return LunarUtil::$ZHI_HIDE_GAN[$this->getYearZhi()]; } /** * 获取年柱五行 * @return string 五行 */ public function getYearWuXing() { return LunarUtil::$WU_XING_GAN[$this->getYearGan()] . LunarUtil::$WU_XING_ZHI[$this->getYearZhi()]; } /** * 获取年柱纳音 * @return string 纳音 */ public function getYearNaYin() { return LunarUtil::$NAYIN[$this->getYear()]; } /** * 获取年柱天干十神 * @return string 十神 */ public function getYearShiShenGan() { return LunarUtil::$SHI_SHEN_GAN[$this->getDayGan() . $this->getYearGan()]; } private function getShiShenZhi($zhi) { $hideGan = LunarUtil::$ZHI_HIDE_GAN[$zhi]; $l = array(); foreach ($hideGan as $gan) { $l[] = LunarUtil::$SHI_SHEN_ZHI[$this->getDayGan() . $zhi . $gan]; } return $l; } /** * 获取年柱地支十神,由于藏干分主气、余气、杂气,所以返回结果可能为1到3个元素 * @return array 十神 */ public function getYearShiShenZhi() { return $this->getShiShenZhi($this->getYearZhi()); } private function getDiShi($zhiIndex) { $offset = EightChar::$CHANG_SHENG_OFFSET[$this->getDayGan()]; $index = $offset + ($this->getDayGanIndex() % 2 == 0 ? $zhiIndex : 0 - $zhiIndex); if ($index >= 12) { $index -= 12; } if ($index < 0) { $index += 12; } return EightChar::$CHANG_SHENG[$index]; } /** * 获取年柱地势(长生十二神) * @return string 地势 */ public function getYearDiShi() { return $this->getDiShi($this->lunar->getYearZhiIndexExact()); } /** * 获取月柱 * @return string 月柱 */ public function getMonth() { return $this->lunar->getMonthInGanZhiExact(); } /** * 获取月干 * @return string 天干 */ public function getMonthGan() { return $this->lunar->getMonthGanExact(); } /** * 获取月支 * @return string 地支 */ public function getMonthZhi() { return $this->lunar->getMonthZhiExact(); } /** * 获取月柱地支藏干,由于藏干分主气、余气、杂气,所以返回结果可能为1到3个元素 * @return string 天干 */ public function getMonthHideGan() { return LunarUtil::$ZHI_HIDE_GAN[$this->getMonthZhi()]; } /** * 获取月柱五行 * @return string 五行 */ public function getMonthWuXing() { return LunarUtil::$WU_XING_GAN[$this->getMonthGan()] . LunarUtil::$WU_XING_ZHI[$this->getMonthZhi()]; } /** * 获取月柱纳音 * @return string 纳音 */ public function getMonthNaYin() { return LunarUtil::$NAYIN[$this->getMonth()]; } /** * 获取月柱天干十神 * @return string 十神 */ public function getMonthShiShenGan() { return LunarUtil::$SHI_SHEN_GAN[$this->getDayGan() . $this->getMonthGan()]; } /** * 获取月柱地支十神,由于藏干分主气、余气、杂气,所以返回结果可能为1到3个元素 * @return array 十神 */ public function getMonthShiShenZhi() { return $this->getShiShenZhi($this->getMonthZhi()); } /** * 获取月柱地势(长生十二神) * @return string 地势 */ public function getMonthDiShi() { return $this->getDiShi($this->lunar->getMonthZhiIndexExact()); } /** * 获取日柱 * @return string 日柱 */ public function getDay() { return (2 == $this->sect) ? $this->lunar->getDayInGanZhiExact2() : $this->lunar->getDayInGanZhiExact(); } /** * 获取日干 * @return string 天干 */ public function getDayGan() { return (2 == $this->sect) ? $this->lunar->getDayGanExact2() : $this->lunar->getDayGanExact(); } /** * 获取日支 * @return string 地支 */ public function getDayZhi() { return (2 == $this->sect) ? $this->lunar->getDayZhiExact2() : $this->lunar->getDayZhiExact(); } /** * 获取日柱地支藏干,由于藏干分主气、余气、杂气,所以返回结果可能为1到3个元素 * @return string 天干 */ public function getDayHideGan() { return LunarUtil::$ZHI_HIDE_GAN[$this->getDayZhi()]; } /** * 获取日柱五行 * @return string 五行 */ public function getDayWuXing() { return LunarUtil::$WU_XING_GAN[$this->getDayGan()] . LunarUtil::$WU_XING_ZHI[$this->getDayZhi()]; } /** * 获取日柱纳音 * @return string 纳音 */ public function getDayNaYin() { return LunarUtil::$NAYIN[$this->getDay()]; } /** * 获取日柱天干十神,也称日元、日干 * @return string 十神 */ public function getDayShiShenGan() { return '日主'; } /** * 获取日柱地支十神,由于藏干分主气、余气、杂气,所以返回结果可能为1到3个元素 * @return array 十神 */ public function getDayShiShenZhi() { return $this->getShiShenZhi($this->getDayZhi()); } /** * 获取日柱天干序号 * @return int 日柱天干序号,0-9 */ public function getDayGanIndex() { return (2 == $this->sect) ? $this->lunar->getDayGanIndexExact2() : $this->lunar->getDayGanIndexExact(); } /** * 获取日柱地支序号 * @return int 日柱地支序号,0-11 */ public function getDayZhiIndex() { return (2 == $this->sect) ? $this->lunar->getDayZhiIndexExact2() : $this->lunar->getDayZhiIndexExact(); } /** * 获取日柱地势(长生十二神) * @return string 地势 */ public function getDayDiShi() { return $this->getDiShi($this->getDayZhiIndex()); } /** * 获取时柱 * @return string 时柱 */ public function getTime() { return $this->lunar->getTimeInGanZhi(); } /** * 获取时干 * @return string 天干 */ public function getTimeGan() { return $this->lunar->getTimeGan(); } /** * 获取时支 * @return string 地支 */ public function getTimeZhi() { return $this->lunar->getTimeZhi(); } /** * 获取时柱地支藏干,由于藏干分主气、余气、杂气,所以返回结果可能为1到3个元素 * @return string 天干 */ public function getTimeHideGan() { return LunarUtil::$ZHI_HIDE_GAN[$this->getTimeZhi()]; } /** * 获取时柱五行 * @return string 五行 */ public function getTimeWuXing() { return LunarUtil::$WU_XING_GAN[$this->lunar->getTimeGan()] . LunarUtil::$WU_XING_ZHI[$this->lunar->getTimeZhi()]; } /** * 获取时柱纳音 * @return string 纳音 */ public function getTimeNaYin() { return LunarUtil::$NAYIN[$this->getTime()]; } /** * 获取时柱天干十神 * @return string 十神 */ public function getTimeShiShenGan() { return LunarUtil::$SHI_SHEN_GAN[$this->getDayGan() . $this->getTimeGan()]; } /** * 获取时柱地支十神,由于藏干分主气、余气、杂气,所以返回结果可能为1到3个元素 * @return array 十神 */ public function getTimeShiShenZhi() { return $this->getShiShenZhi($this->getTimeZhi()); } /** * 获取时柱地势(长生十二神) * @return string 地势 */ public function getTimeDiShi() { return $this->getDiShi($this->lunar->getTimeZhiIndex()); } /** * 获取胎元 * @return string 胎元 */ public function getTaiYuan() { $ganIndex = $this->lunar->getMonthGanIndexExact() + 1; if ($ganIndex >= 10) { $ganIndex -= 10; } $zhiIndex = $this->lunar->getMonthZhiIndexExact() + 3; if ($zhiIndex >= 12) { $zhiIndex -= 12; } return LunarUtil::$GAN[$ganIndex + 1] . LunarUtil::$ZHI[$zhiIndex + 1]; } /** * 获取胎元纳音 * @return string 纳音 */ public function getTaiYuanNaYin() { return LunarUtil::$NAYIN[$this->getTaiYuan()]; } /** * 获取命宫 * @return string 命宫 */ public function getMingGong() { $monthZhiIndex = 0; $timeZhiIndex = 0; for ($i = 0, $j = count(EightChar::$MONTH_ZHI); $i < $j; $i++) { $zhi = EightChar::$MONTH_ZHI[$i]; if ($this->lunar->getMonthZhiExact() == $zhi) { $monthZhiIndex = $i; } if ($this->lunar->getTimeZhi() == $zhi) { $timeZhiIndex = $i; } } $zhiIndex = 26 - ($monthZhiIndex + $timeZhiIndex); if ($zhiIndex > 12) { $zhiIndex -= 12; } $jiaZiIndex = LunarUtil::getJiaZiIndex($this->lunar->getMonthInGanZhiExact()) - ($monthZhiIndex - $zhiIndex); if ($jiaZiIndex >= 60) { $jiaZiIndex -= 60; } if ($jiaZiIndex < 0) { $jiaZiIndex += 60; } return LunarUtil::$JIA_ZI[$jiaZiIndex]; } /** * 获取命宫纳音 * @return string 纳音 */ public function getMingGongNaYin() { return LunarUtil::$NAYIN[$this->getMingGong()]; } /** * 获取身宫 * @return string 身宫 */ public function getShenGong() { $monthZhiIndex = 0; $timeZhiIndex = 0; for ($i = 0, $j = count(EightChar::$MONTH_ZHI); $i < $j; $i++) { $zhi = EightChar::$MONTH_ZHI[$i]; if ($this->lunar->getMonthZhiExact() == $zhi) { $monthZhiIndex = $i; } if ($this->lunar->getTimeZhi() == $zhi) { $timeZhiIndex = $i; } } $zhiIndex = (2 + ($monthZhiIndex + $timeZhiIndex)) % 12; $jiaZiIndex = LunarUtil::getJiaZiIndex($this->lunar->getMonthInGanZhiExact()) - ($monthZhiIndex - $zhiIndex); if ($jiaZiIndex >= 60) { $jiaZiIndex -= 60; } if ($jiaZiIndex < 0) { $jiaZiIndex += 60; } return LunarUtil::$JIA_ZI[$jiaZiIndex]; } /** * 获取身宫纳音 * @return string 纳音 */ public function getShenGongNaYin() { return LunarUtil::$NAYIN[$this->getShenGong()]; } /** * 获取运 * @param int $gender 性别,1男,0女 * @return Yun 运 */ public function getYun($gender) { return new Yun($this, $gender); } /** * 获取年柱所在旬 * @return string 旬 */ public function getYearXun() { return $this->lunar->getYearXunExact(); } /** * 获取年柱旬空(空亡) * @return string 旬空(空亡) */ public function getYearXunKong() { return $this->lunar->getYearXunKongExact(); } /** * 获取月柱所在旬 * @return string 旬 */ public function getMonthXun() { return $this->lunar->getMonthXunExact(); } /** * 获取月柱旬空(空亡) * @return string 旬空(空亡) */ public function getMonthXunKong() { return $this->lunar->getMonthXunKongExact(); } /** * 获取日柱所在旬 * @return string 旬 */ public function getDayXun() { return (2 == $this->sect) ? $this->lunar->getDayXunExact2() : $this->lunar->getDayXunExact(); } /** * 获取日柱旬空(空亡) * @return string 旬空(空亡) */ public function getDayXunKong() { return (2 == $this->sect) ? $this->lunar->getDayXunKongExact2() : $this->lunar->getDayXunKongExact(); } /** * 获取时柱所在旬 * @return string 旬 */ public function getTimeXun() { return $this->lunar->getTimeXun(); } /** * 获取时柱旬空(空亡) * @return string 旬空(空亡) */ public function getTimeXunKong() { return $this->lunar->getTimeXunKong(); } }src/Fu.php000064400000002027146412202250006415 0ustar00name = $name; $this->index = $index; } public function getName() { return $this->name; } public function setName($name) { $this->name = $name; } public function getIndex() { return $this->index; } public function setIndex($index) { $this->index = $index; } public function toString() { return $this->name; } public function __toString() { return $this->toString(); } public function toFullString() { return $this->name . '第' . $this->index . '天'; } } src/Holiday.php000064400000003116146412202250007434 0ustar00day = $day; } else { $this->day = substr($day, 0, 4) . '-' . substr($day, 4, 2) . '-' . substr($day, 6); } $this->name = $name; $this->work = $work; if (strpos($day, '-')) { $this->target = $target; } else { $this->target = substr($target, 0, 4) . '-' . substr($target, 4, 2) . '-' . substr($target, 6); } } public function setDay($day) { $this->day = $day; } public function setName($name) { $this->name = $name; } public function setWork($work) { $this->work = $work; } public function setTarget($target) { $this->target = $target; } public function getDay() { return $this->day; } public function getName() { return $this->name; } public function isWork() { return $this->work; } public function getTarget() { return $this->target; } public function toString() { return $this->day . ' ' . $this->name . ($this->work ? '调休' : '') . ' ' . $this->target; } public function __toString() { return $this->toString(); } } src/JieQi.php000064400000002730146412202250007045 0ustar00setName($name); $this->solar = $solar; } /** * 获取名称 * @return string */ public function getName() { return $this->name; } /** * 设置名称 * @param string $name 名称 */ public function setName($name) { $this->name = $name; foreach (LunarUtil::$JIE as $key) { if ($key == $name) { $this->jie = true; return; } } foreach (LunarUtil::$QI as $key) { if ($key == $name) { $this->qi = true; return; } } } /** * 获取阳历日期 * @return Solar */ public function getSolar() { return $this->solar; } /** * 设置阳历日期 * @param Solar $solar 阳历日期 */ public function setSolar($solar) { $this->solar = $solar; } /** * 是否节令 * @return true/false */ public function isJie() { return $this->jie; } /** * 是否气令 * @return true/false */ public function isQi() { return $this->qi; } public function toString() { return $this->name; } public function __toString() { return $this->toString(); } } src/LiuNian.php000064400000004365146412202250007411 0ustar00daYun = $daYun; $this->lunar = $daYun->getLunar(); $this->index = $index; $this->year = $daYun->getStartYear() + $index; $this->age = $daYun->getStartAge() + $index; } /** * 获取序数 * @return int */ public function getIndex() { return $this->index; } /** * 获取大运 * @return DaYun */ public function getDaYun() { return $this->daYun; } /** * 获取年 * @return int */ public function getYear() { return $this->year; } /** * 获取年龄 * @return int */ public function getAge() { return $this->age; } /** * 获取阴历 * @return Lunar */ public function getLunar() { return $this->lunar; } /** * 获取干支 * @return string */ public function getGanZhi() { $jieQi = $this->lunar->getJieQiTable(); $offset = LunarUtil::getJiaZiIndex($jieQi['立春']->getLunar()->getYearInGanZhiExact()) + $this->index; if ($this->daYun->getIndex() > 0) { $offset += $this->daYun->getStartAge() - 1; } $offset %= count(LunarUtil::$JIA_ZI); return LunarUtil::$JIA_ZI[$offset]; } /** * 获取所在旬 * @return string 旬 */ public function getXun() { return LunarUtil::getXun($this->getGanZhi()); } /** * 获取旬空(空亡) * @return string 旬空(空亡) */ public function getXunKong() { return LunarUtil::getXunKong($this->getGanZhi()); } /** * 获取流月 * @return LiuYue[] */ public function getLiuYue() { $n = 12; $l = array(); for ($i = 0; $i < $n; $i++) { $l[] = new LiuYue($this, $i); } return $l; } } src/LiuYue.php000064400000003606146412202250007263 0ustar00liuNian = $liuNian; $this->index = $index; } /** * 获取序数 * @return int */ public function getIndex() { return $this->index; } /** * 获取流年 * @return LiuNian */ public function getLiuNian() { return $this->liuNian; } /** * 获取中文的月 * @return string 中文月,如正 */ public function getMonthInChinese() { return LunarUtil::$MONTH[$this->index + 1]; } /** * 获取干支 * @return string */ public function getGanZhi() { $offset = 0; $liuNianGanZhi = $this->liuNian->getGanZhi(); $yearGan = substr($liuNianGanZhi, 0, strlen($liuNianGanZhi) / 2); if ('甲' == $yearGan || '己' == $yearGan) { $offset = 2; } else if ('乙' == $yearGan || '庚' == $yearGan) { $offset = 4; } else if ('丙' == $yearGan || '辛' == $yearGan) { $offset = 6; } else if ('丁' == $yearGan || '壬' == $yearGan) { $offset = 8; } $gan = LunarUtil::$GAN[($this->index + $offset) % 10 + 1]; $zhi = LunarUtil::$ZHI[($this->index + LunarUtil::$BASE_MONTH_ZHI_INDEX) % 12 + 1]; return $gan . $zhi; } /** * 获取所在旬 * @return string 旬 */ public function getXun() { return LunarUtil::getXun($this->getGanZhi()); } /** * 获取旬空(空亡) * @return string 旬空(空亡) */ public function getXunKong() { return LunarUtil::getXunKong($this->getGanZhi()); } } src/Lunar.php000064400000302665146412202250007137 0ustar00year = $year; $this->month = $month; $this->day = $day; $this->hour = $hour; $this->minute = $minute; $this->second = $second; $this->dayOffset = LunarUtil::computeAddDays($year, $month, $day); $this->solar = $this->toSolar(); $this->compute(); } /** * 转换为阳历日期 * @return Solar|null */ private function toSolar() { try { $date = new DateTime(SolarUtil::$BASE_YEAR . '-' . SolarUtil::$BASE_MONTH . '-' . SolarUtil::$BASE_DAY . ' ' . $this->hour . ':' . $this->minute . ':' . $this->second); } catch (Exception $e) { return null; } $date->modify($this->dayOffset . ' day'); return Solar::fromDate($date); } /** * 通过指定农历年月日获取农历 * @param int $lunarYear 年(农历) * @param int $lunarMonth 月(农历),1到12,闰月为负,即闰2月=-2 * @param int $lunarDay 日(农历),1到30 * @return Lunar */ public static function fromYmd($lunarYear, $lunarMonth, $lunarDay) { return new Lunar($lunarYear, $lunarMonth, $lunarDay, 0, 0, 0); } /** * 通过指定农历年月日时分秒获取农历 * @param int $lunarYear 年(农历) * @param int $lunarMonth 月(农历),1到12,闰月为负,即闰2月=-2 * @param int $lunarDay 日(农历),1到30 * @param int $hour 小时(阳历) * @param int $minute 分钟(阳历) * @param int $second 秒钟(阳历) * @return Lunar */ public static function fromYmdHms($lunarYear, $lunarMonth, $lunarDay, $hour, $minute, $second) { return new Lunar($lunarYear, $lunarMonth, $lunarDay, $hour, $minute, $second); } /** * 通过阳历日期初始化 * @param DateTime $date 阳历日期 * @return Lunar */ public static function fromDate($date) { $solar = Solar::fromDate($date); $y = $solar->getYear(); $m = $solar->getMonth(); $d = $solar->getDay(); if ($y < 2000) { $startYear = SolarUtil::$BASE_YEAR; $startMonth = SolarUtil::$BASE_MONTH; $startDay = SolarUtil::$BASE_DAY; $lunarYear = LunarUtil::$BASE_YEAR; $lunarMonth = LunarUtil::$BASE_MONTH; $lunarDay = LunarUtil::$BASE_DAY; } else { $startYear = SolarUtil::$BASE_YEAR + 99; $startMonth = 1; $startDay = 1; $lunarYear = LunarUtil::$BASE_YEAR + 99; $lunarMonth = 11; $lunarDay = 25; } $diff = 0; for ($i = $startYear; $i < $y; $i++) { $diff += 365; if (SolarUtil::isLeapYear($i)) { $diff += 1; } } for ($i = $startMonth; $i < $m; $i++) { $diff += SolarUtil::getDaysOfMonth($y, $i); } $diff += $d - $startDay; $lunarDay += $diff; $lastDate = LunarUtil::getDaysOfMonth($lunarYear, $lunarMonth); while ($lunarDay > $lastDate) { $lunarDay -= $lastDate; $lunarMonth = LunarUtil::nextMonth($lunarYear, $lunarMonth); if ($lunarMonth == 1) { $lunarYear++; } $lastDate = LunarUtil::getDaysOfMonth($lunarYear, $lunarMonth); } return new Lunar($lunarYear, $lunarMonth, $lunarDay, $solar->getHour(), $solar->getMinute(), $solar->getSecond()); } private function nutationLon2($t) { $t2 = $t * $t; $dl = 0; for ($i = 0, $j = count(Lunar::$NUT_B); $i < $j; $i += 5) { if ($i == 0) { $a = -1.742 * $t; } else { $a = 0; } $dl += (Lunar::$NUT_B[$i + 3] + $a) * sin(Lunar::$NUT_B[$i] + Lunar::$NUT_B[$i + 1] * $t + Lunar::$NUT_B[$i + 2] * $t2); } return $dl / 100 / (180 * 3600 / pi()); } private function eLon($t, $n) { $t /= 10; $v = 0; $tn = 1; $pn = 1; $m0 = Lunar::$XL0[$pn + 1] - Lunar::$XL0[$pn]; for ($i = 0; $i < 6; $i++, $tn *= $t) { $n1 = intval(floor(Lunar::$XL0[$pn + $i])); $n2 = intval(floor(Lunar::$XL0[$pn + 1 + $i])); $n0 = $n2 - $n1; if ($n0 == 0) { continue; } if ($n < 0) { $m = $n2; } else { $m = intval(floor(3 * $n * $n0 / $m0 + 0.5)) + $n1; if ($i != 0) { $m += 3; } if ($m > $n2) { $m = $n2; } } $c = 0; for ($j = $n1; $j < $m; $j += 3) { $c += Lunar::$XL0[$j] * cos(Lunar::$XL0[$j + 1] + $t * Lunar::$XL0[$j + 2]); } $v += $c * $tn; } $v /= Lunar::$XL0[0]; $t2 = $t * $t; $t3 = $t2 * $t; $v += (-0.0728 - 2.7702 * $t - 1.1019 * $t2 - 0.0996 * $t3) / (180 * 3600 / pi()); return $v; } private function gxcSunLon($t) { $v = -0.043126 + 628.301955 * $t - 0.000002732 * $t * $t; $e = 0.016708634 - 0.000042037 * $t - 0.0000001267 * $t * $t; return -20.49552 * (1 + $e * cos($v)) / (180 * 3600 / pi()); } private function ev($t) { $f = 628.307585 * $t; return 628.332 + 21 * sin(1.527 + $f) + 0.44 * sin(1.48 + $f * 2) + 0.129 * sin(5.82 + $f) * $t + 0.00055 * sin(4.21 + $f) * $t * $t; } private function saLon($t, $n) { return $this->eLon($t, $n) + $this->nutationLon2($t) + $this->gxcSunLon($t) + pi(); } private function dtExt($y, $jsd) { $dy = ($y - 1820) / 100; return -20 + $jsd * $dy * $dy; } private function dtCalc($y) { $size = count(Lunar::$DT_AT); $y0 = Lunar::$DT_AT[$size - 2]; $t0 = Lunar::$DT_AT[$size - 1]; if ($y >= $y0) { $jsd = 31; if ($y > $y0 + 100) { return $this->dtExt($y, $jsd); } $v = $this->dtExt($y, $jsd); $dv = $this->dtExt($y0, $jsd) - $t0; return $v - $dv * ($y0 + 100 - $y) / 100; } for ($i = 0; $i < $size; $i += 5) { if ($y < Lunar::$DT_AT[$i + 5]) { break; } } $t1 = ($y - Lunar::$DT_AT[$i]) / (Lunar::$DT_AT[$i + 5] - Lunar::$DT_AT[$i]) * 10; $t2 = $t1 * $t1; $t3 = $t2 * $t1; return Lunar::$DT_AT[$i + 1] + Lunar::$DT_AT[$i + 2] * $t1 + Lunar::$DT_AT[$i + 3] * $t2 + Lunar::$DT_AT[$i + 4] * $t3; } private function dtT($t) { return $this->dtCalc($t / 365.2425 + 2000) / 86400.0; } private function saLonT($w) { $v = 628.3319653318; $t = ($w - 1.75347 - pi()) / $v; $v = $this->ev($t); $t += ($w - $this->saLon($t, 10)) / $v; $v = $this->ev($t); $t += ($w - $this->saLon($t, -1)) / $v; return $t; } private function qiLow($w) { $v = 628.3319653318; $t = ($w - 4.895062166) / $v; $t -= (53 * $t * $t + 334116 * cos(4.67 + 628.307585 * $t) + 2061 * cos(2.678 + 628.3076 * $t) * $t) / $v / 10000000; $l = 48950621.66 + 6283319653.318 * $t + 53 * $t * $t + 334166 * cos(4.669257 + 628.307585 * $t) + 3489 * cos(4.6261 + 1256.61517 * $t) + 2060.6 * cos(2.67823 + 628.307585 * $t) * $t - 994 - 834 * sin(2.1824 - 33.75705 * $t); $t -= ($l / 10000000 - $w) / 628.332 + (32 * ($t + 1.8) * ($t + 1.8) - 20) / 86400 / 36525; return $t * 36525 + 8 / 24; } private function saLonT2($w) { $pi = pi(); $v = 628.3319653318; $t = ($w - 1.75347 - $pi) / $v; $t -= (0.000005297 * $t * $t + 0.0334166 * cos(4.669257 + 628.307585 * $t) + 0.0002061 * cos(2.67823 + 628.307585 * $t) * $t) / $v; $t += ($w - $this->eLon($t, 8) - $pi + (20.5 + 17.2 * sin(2.1824 - 33.75705 * $t)) / (180 * 3600 / $pi)) / $v; return $t; } private function qiHigh($w) { $t = $this->saLonT2($w) * 36525; $t = $t - $this->dtT($t) + 8 / 24; $v = (($t + 0.5) % 1) * 86400; if ($v < 1200 || $v > 86400 - 1200) { $t = $this->saLonT($w) * 36525 - $this->dtT($t) + 8 / 24; } return $t; } private function calcJieQi($jd) { $size = count(Lunar::$QI_KB); $d = 0; $pc = 7; $jd += 2451545; $f1 = Lunar::$QI_KB[0] - $pc; $f2 = Lunar::$QI_KB[$size - 1] - $pc; $f3 = 2436935; if ($jd < $f1 || $jd >= $f3) { $d = intval(floor($this->qiHigh(intval(floor(($jd + $pc - 2451259) * 24 / 365.2422)) * pi() / 12) + 0.5)); } else if ($jd >= $f1 && $jd < $f2) { $i = 0; for (; $i < $size; $i += 2) { if ($jd + $pc < Lunar::$QI_KB[$i + 2]) { break; } } $d = Lunar::$QI_KB[$i] + Lunar::$QI_KB[$i + 1] * intval(floor(($jd + $pc - Lunar::$QI_KB[$i]) / Lunar::$QI_KB[$i + 1])); $d = intval(floor($d + 0.5)); if ($d == 1683460) { $d++; } $d -= 2451545; } else if ($jd >= $f2 && $jd < $f3) { $d = intval(floor($this->qiLow(intval(floor(($jd + $pc - 2451259) * 24 / 365.2422)) * pi() / 12) + 0.5)); $n = substr(Lunar::$QB, intval(floor(($jd - $f2) / 365.2422 * 24)), 1); if (strcmp('1', $n) == 0) { $d += 1; } if (strcmp('2', $n) == 0) { $d -= 1; } } return $d; } private function qiAccurate($w) { $t = $this->saLonT($w) * 36525; return $t - $this->dtT($t) + 8 / 24; } private function qiAccurate2($jd) { $d = pi() / 12; $w = intval(floor(($jd + 293) * 24 / 365.2422)) * $d; $a = $this->qiAccurate($w); if ($a - $jd > 5) { return $this->qiAccurate($w - $d); } else if ($a - $jd < -5) { return $this->qiAccurate($w + $d); } return $a; } /** * 计算节气表 */ private function computeJieQi() { $jd = intval(floor(($this->solar->getYear() - 2000) * 365.2422 + 180)); $w = intval(floor(($jd - 355 + 183) / 365.2422)) * 365.2422 + 355; if ($this->calcJieQi($w) > $jd) { $w -= 365.2422; } //追加上一农历年末的大雪 $q = $this->calcJieQi($w - 15.2184); $this->jieQi[Lunar::$JIE_QI_PREPEND] = Solar::fromJulianDay($this->qiAccurate2($q) + Solar::$J2000); $size = count(Lunar::$JIE_QI); for ($i = 0; $i < $size; $i++) { $q = $this->calcJieQi($w + 15.2184 * $i); $this->jieQi[Lunar::$JIE_QI[$i]] = Solar::fromJulianDay($this->qiAccurate2($q) + Solar::$J2000); } //追加下一农历年初的冬至 $q = $this->calcJieQi($w + 15.2184 * $size); $this->jieQi[Lunar::$JIE_QI_APPEND] = Solar::fromJulianDay($this->qiAccurate2($q) + Solar::$J2000); //追加下一阳历年初的小寒 $size++; $q = $this->calcJieQi($w + 15.2184 * $size); $this->jieQi[Lunar::$JIE_APPEND_SOLAR_FIRST] = Solar::fromJulianDay($this->qiAccurate2($q) + Solar::$J2000); //追加下一阳历年初的大寒 $size++; $q = $this->calcJieQi($w + 15.2184 * $size); $this->jieQi[Lunar::$QI_APPEND_SOLAR_SECOND] = Solar::fromJulianDay($this->qiAccurate2($q) + Solar::$J2000); } /** * 计算干支纪年 */ private function computeYear() { //以正月初一开始 $yearGanIndex = ($this->year + LunarUtil::$BASE_YEAR_GAN_ZHI_INDEX) % 10; $yearZhiIndex = ($this->year + LunarUtil::$BASE_YEAR_GAN_ZHI_INDEX) % 12; //以立春作为新一年的开始的干支纪年 $g = $yearGanIndex; $z = $yearZhiIndex; //精确的干支纪年,以立春交接时刻为准 $gExact = $yearGanIndex; $zExact = $yearZhiIndex; //获取立春的阳历时刻 $liChun = $this->jieQi['立春']; //阳历和阴历年份相同代表正月初一及以后 if ($this->year === $this->solar->getYear()) { //立春日期判断 if (strcmp($this->solar->toYmd(), $liChun->toYmd()) < 0) { $g--; $z--; } //立春交接时刻判断 if (strcmp($this->solar->toYmdHms(), $liChun->toYmdHms()) < 0) { $gExact--; $zExact--; } } else { if (strcmp($this->solar->toYmd(), $liChun->toYmd()) >= 0) { $g++; $z++; } if (strcmp($this->solar->toYmdHms(), $liChun->toYmdHms()) >= 0) { $gExact++; $zExact++; } } if ($g < 0) { $g += 10; } if ($g >= 10) { $g -= 10; } if ($z < 0) { $z += 12; } if ($z >= 12) { $z -= 12; } if ($gExact < 0) { $gExact += 10; } if ($gExact >= 10) { $gExact -= 10; } if ($zExact < 0) { $zExact += 12; } if ($zExact >= 12) { $zExact -= 12; } $this->yearGanIndex = $yearGanIndex; $this->yearZhiIndex = $yearZhiIndex; $this->yearGanIndexByLiChun = $g; $this->yearZhiIndexByLiChun = $z; $this->yearGanIndexExact = $gExact; $this->yearZhiIndexExact = $zExact; } /** * 干支纪月计算 */ private function computeMonth() { $start = null; //干偏移值(以立春当天起算) $gOffset = (($this->yearGanIndexByLiChun % 5 + 1) * 2) % 10; //干偏移值(以立春交接时刻起算) $gOffsetExact = (($this->yearGanIndexExact % 5 + 1) * 2) % 10; //序号:大雪到小寒之间-2,小寒到立春之间-1,立春之后0 $index = -2; foreach (LunarUtil::$JIE as $jie) { $end = $this->jieQi[$jie]; $ymd = $this->solar->toYmd(); $symd = (null == $start) ? $ymd : $start->toYmd(); $eymd = $end->toYmd(); if (strcmp($ymd, $symd) >= 0 && strcmp($ymd, $eymd) < 0) { break; } $start = $end; $index++; } if ($index < 0) { $index += 12; } $this->monthGanIndex = ($index + $gOffset) % 10; $this->monthZhiIndex = ($index + LunarUtil::$BASE_MONTH_ZHI_INDEX) % 12; $start = null; //序号:大雪到小寒之间-2,小寒到立春之间-1,立春之后0 $indexExact = -2; foreach (LunarUtil::$JIE as $jie) { $end = $this->jieQi[$jie]; $time = $this->solar->toYmdHms(); $stime = null == $start ? $time : $start->toYmdHms(); $etime = $end->toYmdHms(); if (strcmp($time, $stime) >= 0 && strcmp($time, $etime) < 0) { break; } $start = $end; $indexExact++; } if ($indexExact < 0) { $indexExact += 12; } $this->monthGanIndexExact = ($indexExact + $gOffsetExact) % 10; $this->monthZhiIndexExact = ($indexExact + LunarUtil::$BASE_MONTH_ZHI_INDEX) % 12; } /** * 干支纪日计算 */ private function computeDay() { $addDays = ($this->dayOffset + LunarUtil::$BASE_DAY_GAN_ZHI_INDEX) % 60; $dayGanIndex = $addDays % 10; $dayZhiIndex = $addDays % 12; $this->dayGanIndex = $dayGanIndex; $this->dayZhiIndex = $dayZhiIndex; $dayGanExact = $dayGanIndex; $dayZhiExact = $dayZhiIndex; $this->dayGanIndexExact2 = $dayGanExact; $this->dayZhiIndexExact2 = $dayZhiExact; $hm = ($this->hour < 10 ? '0' : '') . $this->hour . ':' . ($this->minute < 10 ? '0' : '') . $this->minute; if (strcmp($hm, '23:00') >= 0 && strcmp($hm, '23:59') <= 0) { $dayGanExact++; if ($dayGanExact >= 10) { $dayGanExact -= 10; } $dayZhiExact++; if ($dayZhiExact >= 12) { $dayZhiExact -= 12; } } $this->dayGanIndexExact = $dayGanExact; $this->dayZhiIndexExact = $dayZhiExact; } /** * 干支纪时计算 */ private function computeTime() { $this->timeZhiIndex = LunarUtil::getTimeZhiIndex(($this->hour < 10 ? '0' : '') . $this->hour . ':' . ($this->minute < 10 ? '0' : '') . $this->minute); $this->timeGanIndex = ($this->dayGanIndexExact % 5 * 2 + $this->timeZhiIndex) % 10; } /** * 星期计算 */ private function computeWeek() { $this->weekIndex = ($this->dayOffset + LunarUtil::$BASE_WEEK_INDEX) % 7; } private function compute() { $this->computeJieQi(); $this->computeYear(); $this->computeMonth(); $this->computeDay(); $this->computeTime(); $this->computeWeek(); } public function getYear() { return $this->year; } public function getMonth() { return $this->month; } public function getDay() { return $this->day; } public function getHour() { return $this->hour; } public function getMinute() { return $this->minute; } public function getSecond() { return $this->second; } public function getSolar() { return $this->solar; } /** * 获取年份的天干(以正月初一作为新年的开始) * @return string 天干,如辛 */ public function getYearGan() { return LunarUtil::$GAN[$this->yearGanIndex + 1]; } /** * 获取年份的天干(以立春当天作为新年的开始) * * @return string 天干,如辛 */ public function getYearGanByLiChun() { return LunarUtil::$GAN[$this->yearGanIndexByLiChun + 1]; } /** * 获取最精确的年份天干(以立春交接的时刻作为新年的开始) * * @return string 天干,如辛 */ public function getYearGanExact() { return LunarUtil::$GAN[$this->yearGanIndexExact + 1]; } /** * 获取年份的地支(以正月初一作为新年的开始) * * @return string 地支,如亥 */ public function getYearZhi() { return LunarUtil::$ZHI[$this->yearZhiIndex + 1]; } /** * 获取年份的地支(以立春当天作为新年的开始) * * @return string 地支,如亥 */ public function getYearZhiByLiChun() { return LunarUtil::$ZHI[$this->yearZhiIndexByLiChun + 1]; } /** * 获取最精确的年份地支(以立春交接的时刻作为新年的开始) * * @return string 地支,如亥 */ public function getYearZhiExact() { return LunarUtil::$ZHI[$this->yearZhiIndexExact + 1]; } /** * 获取干支纪年(年柱)(以正月初一作为新年的开始) * @return string 年份的干支(年柱),如辛亥 */ public function getYearInGanZhi() { return $this->getYearGan() . $this->getYearZhi(); } /** * 获取干支纪年(年柱)(以立春当天作为新年的开始) * @return string 年份的干支(年柱),如辛亥 */ public function getYearInGanZhiByLiChun() { return $this->getYearGanByLiChun() . $this->getYearZhiByLiChun(); } /** * 获取干支纪年(年柱)(以立春交接的时刻作为新年的开始) * @return string 年份的干支(年柱),如辛亥 */ public function getYearInGanZhiExact() { return $this->getYearGanExact() . $this->getYearZhiExact(); } /** * 获取干支纪月(月柱)(以节交接当天起算) *

月天干口诀:甲己丙寅首,乙庚戊寅头。丙辛从庚寅,丁壬壬寅求,戊癸甲寅居,周而复始流。

*

月地支:正月起寅

* * @return string 干支纪月(月柱),如己卯 */ public function getMonthInGanZhi() { return $this->getMonthGan() . $this->getMonthZhi(); } /** * 获取精确的干支纪月(月柱)(以节交接时刻起算) *

月天干口诀:甲己丙寅首,乙庚戊寅头。丙辛从庚寅,丁壬壬寅求,戊癸甲寅居,周而复始流。

*

月地支:正月起寅

* * @return string 干支纪月(月柱),如己卯 */ public function getMonthInGanZhiExact() { return $this->getMonthGanExact() . $this->getMonthZhiExact(); } /** * 获取月天干(以节交接当天起算) * @return string 月天干,如己 */ public function getMonthGan() { return LunarUtil::$GAN[$this->monthGanIndex + 1]; } /** * 获取精确的月天干(以节交接时刻起算) * @return string 月天干,如己 */ public function getMonthGanExact() { return LunarUtil::$GAN[$this->monthGanIndexExact + 1]; } /** * 获取月地支(以节交接当天起算) * @return string 月地支,如卯 */ public function getMonthZhi() { return LunarUtil::$ZHI[$this->monthZhiIndex + 1]; } /** * 获取精确的月地支(以节交接时刻起算) * @return string 月地支,如卯 */ public function getMonthZhiExact() { return LunarUtil::$ZHI[$this->monthZhiIndexExact + 1]; } /** * 获取干支纪日(日柱) * * @return string 干支纪日(日柱),如己卯 */ public function getDayInGanZhi() { return $this->getDayGan() . $this->getDayZhi(); } /** * 获取干支纪日(日柱,八字流派1,晚子时日柱算明天) * @return string 干支纪日(日柱),如己卯 */ public function getDayInGanZhiExact() { return $this->getDayGanExact() . $this->getDayZhiExact(); } /** * 获取干支纪日(日柱,八字流派2,晚子时日柱算当天) * @return string 干支纪日(日柱),如己卯 */ public function getDayInGanZhiExact2() { return $this->getDayGanExact2() . $this->getDayZhiExact2(); } /** * 获取日天干 * * @return string 日天干,如甲 */ public function getDayGan() { return LunarUtil::$GAN[$this->dayGanIndex + 1]; } /** * 获取日天干(八字流派1,晚子时日柱算明天) * @return string 日天干,如甲 */ public function getDayGanExact() { return LunarUtil::$GAN[$this->dayGanIndexExact + 1]; } /** * 获取日天干(八字流派1,晚子时日柱算当天) * @return string 日天干,如甲 */ public function getDayGanExact2() { return LunarUtil::$GAN[$this->dayGanIndexExact2 + 1]; } /** * 获取日地支 * * @return string 日地支,如卯 */ public function getDayZhi() { return LunarUtil::$ZHI[$this->dayZhiIndex + 1]; } /** * 获取日地支(八字流派1,晚子时日柱算明天) * @return string 日地支,如卯 */ public function getDayZhiExact() { return LunarUtil::$ZHI[$this->dayZhiIndexExact + 1]; } /** * 获取日地支(八字流派1,晚子时日柱算当天) * @return string 日地支,如卯 */ public function getDayZhiExact2() { return LunarUtil::$ZHI[$this->dayZhiIndexExact2 + 1]; } /** * 获取年生肖(以正月初一起算) * * @return string 年生肖,如虎 */ public function getYearShengXiao() { return LunarUtil::$SHENG_XIAO[$this->yearZhiIndex + 1]; } /** * 获取年生肖(以立春当天起算) * * @return string 年生肖,如虎 */ public function getYearShengXiaoByLiChun() { return LunarUtil::$SHENG_XIAO[$this->yearZhiIndexByLiChun + 1]; } /** * 获取精确的年生肖(以立春交接时刻起算) * * @return string 年生肖,如虎 */ public function getYearShengXiaoExact() { return LunarUtil::$SHENG_XIAO[$this->yearZhiIndexExact + 1]; } /** * 获取月生肖 * * @return string 月生肖,如虎 */ public function getMonthShengXiao() { return LunarUtil::$SHENG_XIAO[$this->monthZhiIndex + 1]; } /** * 获取日生肖 * * @return string 日生肖,如虎 */ public function getDayShengXiao() { return LunarUtil::$SHENG_XIAO[$this->dayZhiIndex + 1]; } /** * 获取时辰生肖 * * @return string 时辰生肖,如虎 */ public function getTimeShengXiao() { return LunarUtil::$SHENG_XIAO[$this->timeZhiIndex + 1]; } /** * 获取中文的年 * * @return string 中文年,如二零零一 */ public function getYearInChinese() { $y = ($this->year . ''); $s = ''; for ($i = 0, $j = strlen($y); $i < $j; $i++) { $s .= LunarUtil::$NUMBER[ord(substr($y, $i, 1)) - 48]; } return $s; } /** * 获取中文的月 * * @return string 中文月,如正 */ public function getMonthInChinese() { return ($this->month < 0 ? '闰' : '') . LunarUtil::$MONTH[abs($this->month)]; } /** * 获取中文日 * * @return string 中文日,如初一 */ public function getDayInChinese() { return LunarUtil::$DAY[$this->day]; } /** * 获取时辰(地支) * @return string 时辰(地支) */ public function getTimeZhi() { return LunarUtil::$ZHI[$this->timeZhiIndex + 1]; } /** * 获取时辰(天干) * @return string 时辰(天干) */ public function getTimeGan() { return LunarUtil::$GAN[$this->timeGanIndex + 1]; } /** * 获取时辰干支(时柱) * @return string 时辰干支(时柱) */ public function getTimeInGanZhi() { return $this->getTimeGan() . $this->getTimeZhi(); } /** * 获取季节 * @return string 农历季节 */ public function getSeason() { return LunarUtil::$SEASON[abs($this->month)]; } /** * 获取节 * * @return string 节 */ public function getJie() { foreach (LunarUtil::$JIE as $jie) { $d = $this->jieQi[$jie]; if ($d->getYear() === $this->solar->getYear() && $d->getMonth() === $this->solar->getMonth() && $d->getDay() === $this->solar->getDay()) { return $jie; } } $d = $this->jieQi[Lunar::$JIE_QI_PREPEND]; if ($d->getYear() === $this->solar->getYear() && $d->getMonth() === $this->solar->getMonth() && $d->getDay() === $this->solar->getDay()) { return Lunar::$JIE_QI_LAST; } $d = $this->jieQi[Lunar::$JIE_APPEND_SOLAR_FIRST]; if ($d->getYear() === $this->solar->getYear() && $d->getMonth() === $this->solar->getMonth() && $d->getDay() === $this->solar->getDay()) { return Lunar::$JIE_SOLAR_FIRST; } return ''; } /** * 获取气 * * @return string 气 */ public function getQi() { foreach (LunarUtil::$QI as $qi) { $d = $this->jieQi[$qi]; if ($d->getYear() === $this->solar->getYear() && $d->getMonth() === $this->solar->getMonth() && $d->getDay() === $this->solar->getDay()) { return $qi; } } $d = $this->jieQi[Lunar::$JIE_QI_APPEND]; if ($d->getYear() === $this->solar->getYear() && $d->getMonth() === $this->solar->getMonth() && $d->getDay() === $this->solar->getDay()) { return Lunar::$JIE_QI_FIRST; } $d = $this->jieQi[Lunar::$QI_APPEND_SOLAR_SECOND]; if ($d->getYear() === $this->solar->getYear() && $d->getMonth() === $this->solar->getMonth() && $d->getDay() === $this->solar->getDay()) { return Lunar::$QI_SOLAR_SECOND; } return ''; } /** * 获取星期,0代表周日,1代表周一 * * @return int 0123456 */ public function getWeek() { return $this->weekIndex; } /** * 获取星期的中文 * * @return string 日一二三四五六 */ public function getWeekInChinese() { return SolarUtil::$WEEK[$this->getWeek()]; } /** * 获取宿 * * @return string 宿 */ public function getXiu() { return LunarUtil::$XIU[$this->getDayZhi() . $this->getWeek()]; } /** * 获取宿吉凶 * * @return string 吉/凶 */ public function getXiuLuck() { return LunarUtil::$XIU_LUCK[$this->getXiu()]; } /** * 获取宿歌诀 * * @return string 宿歌诀 */ public function getXiuSong() { return LunarUtil::$XIU_SONG[$this->getXiu()]; } /** * 获取政 * * @return string 政 */ public function getZheng() { return LunarUtil::$ZHENG[$this->getXiu()]; } /** * 获取动物 * @return string 动物 */ public function getAnimal() { return LunarUtil::$ANIMAL[$this->getXiu()]; } /** * 获取宫 * @return string 宫 */ public function getGong() { return LunarUtil::$GONG[$this->getXiu()]; } /** * 获取兽 * @return string 兽 */ public function getShou() { return LunarUtil::$SHOU[$this->getGong()]; } /** * 获取节日,有可能一天会有多个节日 * * @return array 节日列表,如春节 */ public function getFestivals() { $l = array(); if (!empty(LunarUtil::$FESTIVAL[$this->month . '-' . $this->day])) { $l[] = LunarUtil::$FESTIVAL[$this->month . '-' . $this->day]; } return $l; } /** * 获取非正式的节日,有可能一天会有多个节日 * * @return array 非正式的节日列表,如中元节 */ public function getOtherFestivals() { $l = array(); if (!empty(LunarUtil::$OTHER_FESTIVAL[$this->month . '-' . $this->day])) { $l[] = LunarUtil::$OTHER_FESTIVAL[$this->month . '-' . $this->day]; } return $l; } /** * 获取彭祖百忌天干 * @return string 彭祖百忌天干 */ public function getPengZuGan() { return LunarUtil::$PENG_ZU_GAN[$this->dayGanIndex + 1]; } /** * 获取彭祖百忌地支 * @return string 彭祖百忌地支 */ public function getPengZuZhi() { return LunarUtil::$PENG_ZU_ZHI[$this->dayZhiIndex + 1]; } /** * 获取日喜神方位 * @return string 喜神方位,如艮 */ public function getPositionXi() { return $this->getDayPositionXi(); } /** * 获取日喜神方位描述 * @return string 喜神方位描述,如东北 */ public function getPositionXiDesc() { return $this->getDayPositionXiDesc(); } /** * 获取日喜神方位 * @return string 喜神方位,如艮 */ public function getDayPositionXi() { return LunarUtil::$POSITION_XI[$this->dayGanIndex + 1]; } /** * 获取日喜神方位描述 * @return string 喜神方位描述,如东北 */ public function getDayPositionXiDesc() { return LunarUtil::$POSITION_DESC[$this->getDayPositionXi()]; } /** * 获取时辰喜神方位 * @return string 喜神方位,如艮 */ public function getTimePositionXi() { return LunarUtil::$POSITION_XI[$this->timeGanIndex + 1]; } /** * 获取时辰喜神方位描述 * @return string 喜神方位描述,如东北 */ public function getTimePositionXiDesc() { return LunarUtil::$POSITION_DESC[$this->getTimePositionXi()]; } /** * 获取日阳贵神方位 * @return string 阳贵神方位,如艮 */ public function getPositionYangGui() { return $this->getDayPositionYangGui(); } /** * 获取日阳贵神方位描述 * @return string 阳贵神方位描述,如东北 */ public function getPositionYangGuiDesc() { return $this->getDayPositionYangGuiDesc(); } /** * 获取日阳贵神方位 * @return string 阳贵神方位,如艮 */ public function getDayPositionYangGui() { return LunarUtil::$POSITION_YANG_GUI[$this->dayGanIndex + 1]; } /** * 获取日阳贵神方位描述 * @return string 阳贵神方位描述,如东北 */ public function getDayPositionYangGuiDesc() { return LunarUtil::$POSITION_DESC[$this->getDayPositionYangGui()]; } /** * 获取时辰阳贵神方位 * @return string 阳贵神方位,如艮 */ public function getTimePositionYangGui() { return LunarUtil::$POSITION_YANG_GUI[$this->timeGanIndex + 1]; } /** * 获取时辰阳贵神方位描述 * @return string 阳贵神方位描述,如东北 */ public function getTimePositionYangGuiDesc() { return LunarUtil::$POSITION_DESC[$this->getTimePositionYangGui()]; } /** * 获取日阴贵神方位 * @return string 阴贵神方位,如艮 */ public function getPositionYinGui() { return $this->getDayPositionYinGui(); } /** * 获取日阴贵神方位描述 * @return string 阴贵神方位描述,如东北 */ public function getPositionYinGuiDesc() { return $this->getDayPositionYinGuiDesc(); } /** * 获取日阴贵神方位 * @return string 阴贵神方位,如艮 */ public function getDayPositionYinGui() { return LunarUtil::$POSITION_YIN_GUI[$this->dayGanIndex + 1]; } /** * 获取日阴贵神方位描述 * @return string 阴贵神方位描述,如东北 */ public function getDayPositionYinGuiDesc() { return LunarUtil::$POSITION_DESC[$this->getDayPositionYinGui()]; } /** * 获取时辰阴贵神方位 * @return string 阴贵神方位,如艮 */ public function getTimePositionYinGui() { return LunarUtil::$POSITION_YIN_GUI[$this->timeGanIndex + 1]; } /** * 获取时辰阴贵神方位描述 * @return string 阴贵神方位描述,如东北 */ public function getTimePositionYinGuiDesc() { return LunarUtil::$POSITION_DESC[$this->getTimePositionYinGui()]; } /** * 获取日福神方位 * @return string 福神方位,如艮 */ public function getPositionFu() { return $this->getDayPositionFu(); } /** * 获取日福神方位描述 * @return string 福神方位描述,如东北 */ public function getPositionFuDesc() { return $this->getDayPositionFuDesc(); } /** * 获取日福神方位 * @return string 福神方位,如艮 */ public function getDayPositionFu() { return LunarUtil::$POSITION_FU[$this->dayGanIndex + 1]; } /** * 获取日福神方位描述 * @return string 福神方位描述,如东北 */ public function getDayPositionFuDesc() { return LunarUtil::$POSITION_DESC[$this->getDayPositionFu()]; } /** * 获取时辰福神方位 * @return string 福神方位,如艮 */ public function getTimePositionFu() { return LunarUtil::$POSITION_FU[$this->timeGanIndex + 1]; } /** * 获取时辰福神方位描述 * @return string 福神方位描述,如东北 */ public function getTimePositionFuDesc() { return LunarUtil::$POSITION_DESC[$this->getTimePositionFu()]; } /** * 获取日财神方位 * @return string 财神方位,如艮 */ public function getPositionCai() { return $this->getDayPositionCai(); } /** * 获取日财神方位描述 * @return string 财神方位描述,如东北 */ public function getPositionCaiDesc() { return $this->getDayPositionCaiDesc(); } /** * 获取日财神方位 * @return string 财神方位,如艮 */ public function getDayPositionCai() { return LunarUtil::$POSITION_CAI[$this->dayGanIndex + 1]; } /** * 获取日财神方位描述 * @return string 财神方位描述,如东北 */ public function getDayPositionCaiDesc() { return LunarUtil::$POSITION_DESC[$this->getDayPositionCai()]; } /** * 获取时辰财神方位 * @return string 财神方位,如艮 */ public function getTimePositionCai() { return LunarUtil::$POSITION_CAI[$this->timeGanIndex + 1]; } /** * 获取时辰财神方位描述 * @return string 财神方位描述,如东北 */ public function getTimePositionCaiDesc() { return LunarUtil::$POSITION_DESC[$this->getTimePositionCai()]; } /** * 获取冲 * @return string 冲,如申 */ public function getChong() { return $this->getDayChong(); } /** * 获取日冲 * @return string 日冲,如申 */ public function getDayChong() { return LunarUtil::$CHONG[$this->dayZhiIndex + 1]; } /** * 获取时冲 * @return string 时冲,如申 */ public function getTimeChong() { return LunarUtil::$CHONG[$this->timeZhiIndex + 1]; } /** * 获取无情之克的冲天干 * @return string 无情之克的冲天干,如甲 */ public function getChongGan() { return $this->getDayChongGan(); } /** * 获取无情之克的日冲天干 * @return string 无情之克的日冲天干,如甲 */ public function getDayChongGan() { return LunarUtil::$CHONG_GAN[$this->dayGanIndex + 1]; } /** * 获取无情之克的时冲天干 * @return string 无情之克的时冲天干,如甲 */ public function getTimeChongGan() { return LunarUtil::$CHONG_GAN[$this->timeGanIndex + 1]; } /** * 获取有情之克的冲天干 * @return string 有情之克的冲天干,如甲 */ public function getChongGanTie() { return $this->getDayChongGanTie(); } /** * 获取有情之克的日冲天干 * @return string 有情之克的日冲天干,如甲 */ public function getDayChongGanTie() { return LunarUtil::$CHONG_GAN_TIE[$this->dayGanIndex + 1]; } /** * 获取有情之克的时冲天干 * @return string 有情之克的时冲天干,如甲 */ public function getTimeChongGanTie() { return LunarUtil::$CHONG_GAN_TIE[$this->timeGanIndex + 1]; } /** * 获取冲生肖 * @return string 冲生肖,如猴 */ public function getChongShengXiao() { return $this->getDayChongShengXiao(); } /** * 获取日冲生肖 * @return string 日冲生肖,如猴 */ public function getDayChongShengXiao() { $chong = $this->getDayChong(); for ($i = 0, $j = count(LunarUtil::$ZHI); $i < $j; $i++) { if (strcmp(LunarUtil::$ZHI[$i], $chong) === 0) { return LunarUtil::$SHENG_XIAO[$i]; } } return ''; } /** * 获取时冲生肖 * @return string 时冲生肖,如猴 */ public function getTimeChongShengXiao() { $chong = $this->getTimeChong(); for ($i = 0, $j = count(LunarUtil::$ZHI); $i < $j; $i++) { if (strcmp(LunarUtil::$ZHI[$i], $chong) === 0) { return LunarUtil::$SHENG_XIAO[$i]; } } return ''; } /** * 获取冲描述 * @return string 冲描述,如(壬申)猴 */ public function getChongDesc() { return $this->getDayChongDesc(); } /** * 获取日冲描述 * @return string 日冲描述,如(壬申)猴 */ public function getDayChongDesc() { return '(' . $this->getDayChongGan() . $this->getDayChong() . ')' . $this->getDayChongShengXiao(); } /** * 获取时冲描述 * @return string 时冲描述,如(壬申)猴 */ public function getTimeChongDesc() { return '(' . $this->getTimeChongGan() . $this->getTimeChong() . ')' . $this->getTimeChongShengXiao(); } /** * 获取煞 * @return string 煞,如北 */ public function getSha() { return $this->getDaySha(); } /** * 获取日煞 * @return string 日煞,如北 */ public function getDaySha() { return LunarUtil::$SHA[$this->getDayZhi()]; } /** * 获取时煞 * @return string 时煞,如北 */ public function getTimeSha() { return LunarUtil::$SHA[$this->getTimeZhi()]; } /** * 获取年纳音 * @return string 年纳音,如剑锋金 */ public function getYearNaYin() { return LunarUtil::$NAYIN[$this->getYearInGanZhi()]; } /** * 获取月纳音 * @return string 月纳音,如剑锋金 */ public function getMonthNaYin() { return LunarUtil::$NAYIN[$this->getMonthInGanZhi()]; } /** * 获取日纳音 * @return string 日纳音,如剑锋金 */ public function getDayNaYin() { return LunarUtil::$NAYIN[$this->getDayInGanZhi()]; } /** * 获取时辰纳音 * @return string 时辰纳音,如剑锋金 */ public function getTimeNaYin() { return LunarUtil::$NAYIN[$this->getTimeInGanZhi()]; } /** * 获取八字,男性也称乾造,女性也称坤造(以立春交接时刻作为新年的开始) * @return array 八字(男性也称乾造,女性也称坤造) */ public function getBaZi() { $baZi = $this->getEightChar(); $l = array(); $l[] = $baZi->getYear(); $l[] = $baZi->getMonth(); $l[] = $baZi->getDay(); $l[] = $baZi->getTime(); return $l; } /** * 获取八字五行 * @return array 八字五行 */ public function getBaZiWuXing() { $baZi = $this->getEightChar(); $l = array(); $l[] = $baZi->getYearWuXing(); $l[] = $baZi->getMonthWuXing(); $l[] = $baZi->getDayWuXing(); $l[] = $baZi->getTimeWuXing(); return $l; } /** * 获取八字纳音 * @return array 八字纳音 */ public function getBaZiNaYin() { $baZi = $this->getEightChar(); $l = array(); $l[] = $baZi->getYearNaYin(); $l[] = $baZi->getMonthNaYin(); $l[] = $baZi->getDayNaYin(); $l[] = $baZi->getTimeNaYin(); return $l; } /** * 获取八字天干十神,日柱十神为日主,其余三柱根据天干十神表查询 * @return array 八字天干十神 */ public function getBaZiShiShenGan() { $baZi = $this->getEightChar(); $l = array(); $l[] = $baZi->getYearShiShenGan(); $l[] = $baZi->getMonthShiShenGan(); $l[] = $baZi->getDayShiShenGan(); $l[] = $baZi->getTimeShiShenGan(); return $l; } /** * 获取八字地支十神,根据地支十神表查询 * @return array 八字地支十神 */ public function getBaZiShiShenZhi() { $baZi = $this->getEightChar(); $yearShiShenZhi = $baZi->getYearShiShenZhi(); $monthShiShenZhi = $baZi->getMonthShiShenZhi(); $dayShiShenZhi = $baZi->getDayShiShenZhi(); $timeShiShenZhi = $baZi->getTimeShiShenZhi(); $l = array(); $l[] = $yearShiShenZhi[0]; $l[] = $monthShiShenZhi[0]; $l[] = $dayShiShenZhi[0]; $l[] = $timeShiShenZhi[0]; return $l; } /** * 获取八字年支十神 * @return array 八字年支十神 */ public function getBaZiShiShenYearZhi() { return $this->getEightChar()->getYearShiShenZhi(); } /** * 获取八字月支十神 * @return array 八字月支十神 */ public function getBaZiShiShenMonthZhi() { return $this->getEightChar()->getMonthShiShenZhi(); } /** * 获取八字日支十神 * @return array 八字日支十神 */ public function getBaZiShiShenDayZhi() { return $this->getEightChar()->getDayShiShenZhi(); } /** * 获取八字时支十神 * @return array 八字时支十神 */ public function getBaZiShiShenTimeZhi() { return $this->getEightChar()->getTimeShiShenZhi(); } /** * 获取十二执星:建、除、满、平、定、执、破、危、成、收、开、闭。当月支与日支相同即为建,依次类推 * @return string 执星 */ public function getZhiXing() { $offset = $this->dayZhiIndex - $this->monthZhiIndex; if ($offset < 0) { $offset += 12; } return LunarUtil::$ZHI_XING[$offset + 1]; } /** * 获取值日天神 * @return string 值日天神 */ public function getDayTianShen() { $monthZhi = $this->getMonthZhi(); $offset = LunarUtil::$ZHI_TIAN_SHEN_OFFSET[$monthZhi]; return LunarUtil::$TIAN_SHEN[($this->dayZhiIndex + $offset) % 12 + 1]; } /** * 获取值时天神 * @return string 值时天神 */ public function getTimeTianShen() { $dayZhi = $this->getDayZhiExact(); $offset = LunarUtil::$ZHI_TIAN_SHEN_OFFSET[$dayZhi]; return LunarUtil::$TIAN_SHEN[($this->timeZhiIndex + $offset) % 12 + 1]; } /** * 获取值日天神类型:黄道/黑道 * @return string 值日天神类型:黄道/黑道 */ public function getDayTianShenType() { return LunarUtil::$TIAN_SHEN_TYPE[$this->getDayTianShen()]; } /** * 获取值时天神类型:黄道/黑道 * @return string 值时天神类型:黄道/黑道 */ public function getTimeTianShenType() { return LunarUtil::$TIAN_SHEN_TYPE[$this->getTimeTianShen()]; } /** * 获取值日天神吉凶 * @return string 吉/凶 */ public function getDayTianShenLuck() { return LunarUtil::$TIAN_SHEN_TYPE_LUCK[$this->getDayTianShenType()]; } /** * 获取值时天神吉凶 * @return string 吉/凶 */ public function getTimeTianShenLuck() { return LunarUtil::$TIAN_SHEN_TYPE_LUCK[$this->getTimeTianShenType()]; } /** * 获取逐日胎神方位 * @return string 逐日胎神方位 */ public function getDayPositionTai() { $offset = $this->dayGanIndex - $this->dayZhiIndex; if ($offset < 0) { $offset += 12; } return LunarUtil::$POSITION_TAI_DAY[$offset * 5 + $this->dayGanIndex]; } /** * 获取逐月胎神方位,闰月无 * @return string 逐月胎神方位 */ public function getMonthPositionTai() { if ($this->month < 0) { return ''; } return LunarUtil::$POSITION_TAI_MONTH[$this->month - 1]; } /** * 获取每日宜 * @return array 宜 */ public function getDayYi() { return LunarUtil::getDayYi($this->getMonthInGanZhiExact(), $this->getDayInGanZhi()); } /** * 获取时宜 * @return array 宜 */ public function getTimeYi() { return LunarUtil::getTimeYi($this->getDayInGanZhiExact(), $this->getTimeInGanZhi()); } /** * 获取每日忌 * @return array 忌 */ public function getDayJi() { return LunarUtil::getDayJi($this->getMonthInGanZhiExact(), $this->getDayInGanZhi()); } /** * 获取时忌 * @return array 忌 */ public function getTimeJi() { return LunarUtil::getTimeJi($this->getDayInGanZhiExact(), $this->getTimeInGanZhi()); } /** * 获取日吉神(宜趋),如果没有,返回['无'] * @return array 吉神 */ public function getDayJiShen() { return LunarUtil::getDayJiShen($this->getMonth(), $this->getDayInGanZhi()); } /** * 获取日凶煞(宜忌),如果没有,返回['无'] * @return array 凶煞 */ public function getDayXiongSha() { return LunarUtil::getDayXiongSha($this->getMonth(), $this->getDayInGanZhi()); } /** * 获取月相 * @return string 月相 */ public function getYueXiang() { return LunarUtil::$YUE_XIANG[$this->getDay()]; } /** * 获取值年九星(流年紫白星起例歌诀:年上吉星论甲子,逐年星逆中宫起;上中下作三元汇,一上四中七下兑。) * @return NineStar 值年九星 */ public function getYearNineStar() { $index = LunarUtil::$BASE_YEAR_JIU_XING_INDEX - ($this->getYear() - LunarUtil::$BASE_YEAR) % 9; if ($index < 0) { $index += 9; } return new NineStar($index); } /** * 获取值月九星(月紫白星歌诀:子午卯酉八白起,寅申巳亥二黑求,辰戌丑未五黄中。) * @return NineStar 值月九星 */ public function getMonthNineStar() { $start = 2; $yearZhi = $this->getYearZhi(); if (strpos('子午卯酉', $yearZhi) !== false) { $start = 8; } else if (strpos('辰戌丑未', $yearZhi) !== false) { $start = 5; } // 寅月起,所以需要-2 $monthIndex = $this->monthZhiIndex - 2; if ($monthIndex < 0) { $monthIndex += 12; } $index = $start - $monthIndex - 1; while ($index < 0) { $index += 9; } return new NineStar($index); } /** * 获取值日九星(日家紫白星歌诀:日家白法不难求,二十四气六宫周;冬至雨水及谷雨,阳顺一七四中游;夏至处暑霜降后,九三六星逆行求。) * @return NineStar 值日九星 */ public function getDayNineStar() { //顺逆 $solarYmd = $this->solar->toYmd(); $yuShui = $this->jieQi['雨水']->toYmd(); $guYu = $this->jieQi['谷雨']->toYmd(); $xiaZhi = $this->jieQi['夏至']->toYmd(); $chuShu = $this->jieQi['处暑']->toYmd(); $shuangJiang = $this->jieQi['霜降']->toYmd(); $start = 6; $asc = false; if (strcmp($solarYmd, $this->jieQi['冬至']->toYmd()) >= 0 && strcmp($solarYmd, $yuShui) < 0) { $asc = true; $start = 1; } else if (strcmp($solarYmd, $yuShui) >= 0 && strcmp($solarYmd, $guYu) < 0) { $asc = true; $start = 7; } else if (strcmp($solarYmd, $guYu) >= 0 && strcmp($solarYmd, $xiaZhi) < 0) { $asc = true; $start = 4; } else if (strcmp($solarYmd, $xiaZhi) >= 0 && strcmp($solarYmd, $chuShu) < 0) { $start = 9; } else if (strcmp($solarYmd, $chuShu) >= 0 && strcmp($solarYmd, $shuangJiang) < 0) { $start = 3; } $ganZhiIndex = LunarUtil::getJiaZiIndex($this->getDayInGanZhi()) % 9; $index = $asc ? $start + $ganZhiIndex - 1 : $start - $ganZhiIndex - 1; if ($index > 8) { $index -= 9; } if ($index < 0) { $index += 9; } return new NineStar($index); } /** * 获取值时九星(时家紫白星歌诀:三元时白最为佳,冬至阳生顺莫差,孟日七宫仲一白,季日四绿发萌芽,每把时辰起甲子,本时星耀照光华,时星移入中宫去,顺飞八方逐细查。夏至阴生逆回首,孟归三碧季加六,仲在九宫时起甲,依然掌中逆轮跨。) * @return NineStar 值时九星 */ public function getTimeNineStar() { //顺逆 $solarYmd = $this->solar->toYmd(); $asc = false; if (strcmp($solarYmd, $this->jieQi['冬至']->toYmd()) >= 0 && strcmp($solarYmd, $this->jieQi['夏至']->toYmd()) < 0) { $asc = true; } $start = $asc ? 7 : 3; $dayZhi = $this->getDayZhi(); if (strpos('子午卯酉', $dayZhi) !== false) { $start = $asc ? 1 : 9; } else if (strpos('辰戌丑未', $dayZhi) !== false) { $start = $asc ? 4 : 6; } $index = $asc ? $start + $this->timeZhiIndex - 1 : $start - $this->timeZhiIndex - 1; if ($index > 8) { $index -= 9; } if ($index < 0) { $index += 9; } return new NineStar($index); } /** * 获取节气表(节气名称:阳历),节气交接时刻精确到秒,以冬至开头,按先后顺序排列 * @return Solar[] 节气表 */ public function getJieQiTable() { return $this->jieQi; } /** * 获取最近的节气,如果未找到匹配的,返回null * @param $forward bool 是否顺推,true为顺推,false为逆推 * @param $conditions array 过滤条件,如果设置过滤条件,仅返回匹配该名称的 * @return JieQi|null 节气 */ protected function getNearJieQi($forward, array $conditions) { $name = null; $near = null; $filter = null != $conditions && count($conditions) > 0; $today = $this->solar->toYmdHms(); foreach ($this->jieQi as $jq => $solar) { if (Lunar::$JIE_QI_APPEND == $jq) { $jq = Lunar::$JIE_QI_FIRST; } if (Lunar::$JIE_QI_PREPEND == $jq) { $jq = Lunar::$JIE_QI_LAST; } if (Lunar::$JIE_APPEND_SOLAR_FIRST == $jq) { $jq = Lunar::$JIE_SOLAR_FIRST; } if (Lunar::$QI_APPEND_SOLAR_SECOND == $jq) { $jq = Lunar::$QI_SOLAR_SECOND; } if ($filter) { if (!in_array($jq, $conditions)) { continue; } } $day = $solar->toYmdHms(); if ($forward) { if (strcmp($day, $today) < 0) { continue; } if (null == $near || strcmp($day, $near) < 0) { $name = $jq; $near = $solar; } } else { if (strcmp($day, $today) > 0) { continue; } if (null == $near || strcmp($day, $near) > 0) { $name = $jq; $near = $solar; } } } if (null == $near) { return null; } return new JieQi($name, $near); } /** * 获取下一节(顺推的第一个节) * @return JieQi|null 节气 */ public function getNextJie() { return $this->getNearJieQi(true, LunarUtil::$JIE); } /** * 获取上一节(逆推的第一个节) * @return JieQi|null 节气 */ public function getPrevJie() { return $this->getNearJieQi(false, LunarUtil::$JIE); } /** * 获取下一气令(顺推的第一个气令) * @return JieQi|null 节气 */ public function getNextQi() { return $this->getNearJieQi(true, LunarUtil::$QI); } /** * 获取上一气令(逆推的第一个气令) * @return JieQi|null 节气 */ public function getPrevQi() { return $this->getNearJieQi(false, LunarUtil::$QI); } /** * 获取下一节气(顺推的第一个节气) * @return JieQi|null 节气 */ public function getNextJieQi() { return $this->getNearJieQi(true, null); } /** * 获取上一节气(逆推的第一个节气) * @return JieQi|null 节气 */ public function getPrevJieQi() { return $this->getNearJieQi(false, null); } /** * 获取节气名称,如果无节气,返回空字符串 * @return string 节气名称 */ public function getJieQi() { $name = ''; foreach ($this->jieQi as $jq => $d) { if ($d->getYear() == $this->solar->getYear() && $d->getMonth() == $this->solar->getMonth() && $d->getDay() == $this->solar->getDay()) { $name = $jq; break; } } if (Lunar::$JIE_QI_APPEND == $name) { $name = Lunar::$JIE_QI_FIRST; } else if (Lunar::$JIE_QI_PREPEND == $name) { $name = Lunar::$JIE_QI_LAST; } else if (Lunar::$JIE_APPEND_SOLAR_FIRST == $name) { $name = Lunar::$JIE_SOLAR_FIRST; } else if (Lunar::$QI_APPEND_SOLAR_SECOND == $name) { $name = Lunar::$QI_SOLAR_SECOND; } return $name; } /** * 获取当天节气对象,如果无节气,返回null * @return JieQi|null 节气对象 */ public function getCurrentJieQi() { $name = $this->getJieQi(); return strlen($name) > 0 ? new JieQi($name, $this->solar) : null; } /** * 获取当天节令对象,如果无节令,返回null * @return JieQi|null 节气对象 */ public function getCurrentJie() { $name = $this->getJie(); return strlen($name) > 0 ? new JieQi($name, $this->solar) : null; } /** * 获取当天气令对象,如果无气令,返回null * @return JieQi|null 节气对象 */ public function getCurrentQi() { $name = $this->getQi(); return strlen($name) > 0 ? new JieQi($name, $this->solar) : null; } public function getTimeGanIndex() { return $this->timeGanIndex; } public function getTimeZhiIndex() { return $this->timeZhiIndex; } public function getDayGanIndex() { return $this->dayGanIndex; } public function getDayZhiIndex() { return $this->dayZhiIndex; } public function getMonthGanIndex() { return $this->monthGanIndex; } public function getMonthZhiIndex() { return $this->monthZhiIndex; } public function getYearGanIndex() { return $this->yearGanIndex; } public function getYearZhiIndex() { return $this->yearZhiIndex; } public function getYearGanIndexByLiChun() { return $this->yearGanIndexByLiChun; } public function getYearZhiIndexByLiChun() { return $this->yearZhiIndexByLiChun; } public function getDayGanIndexExact() { return $this->dayGanIndexExact; } public function getDayGanIndexExact2() { return $this->dayGanIndexExact2; } public function getDayZhiIndexExact() { return $this->dayZhiIndexExact; } public function getDayZhiIndexExact2() { return $this->dayZhiIndexExact2; } public function getMonthGanIndexExact() { return $this->monthGanIndexExact; } public function getMonthZhiIndexExact() { return $this->monthZhiIndexExact; } public function getYearGanIndexExact() { return $this->yearGanIndexExact; } public function getYearZhiIndexExact() { return $this->yearZhiIndexExact; } public function getEightChar() { if (null == $this->eightChar) { $this->eightChar = EightChar::fromLunar($this); } return $this->eightChar; } /** * 获取往后推几天的农历日期,如果要往前推,则天数用负数 * @param int days 天数 * @return Lunar 农历日期 */ public function next($days) { $y = $this->year; $m = $this->month; $d = $this->day; if ($days > 0) { $daysInMonth = LunarUtil::getDaysOfMonth($y, $m); $rest = $this->day + $days; while ($daysInMonth < $rest) { if ($m > 0) { if (LunarUtil::getLeapMonth($y) != $m) { $m++; } else { $m = -$m; } } else { $m = 1 - $m; } if (13 == $m) { $y++; $m = 1; } $rest -= $daysInMonth; $daysInMonth = LunarUtil::getDaysOfMonth($y, $m); } $d = $rest; } else if ($days < 0) { $daysInMonth = $this->day; $rest = -$days; while ($daysInMonth <= $rest) { if ($m > 0) { $m--; if (0 == $m) { $y--; $m = LunarUtil::getLeapMonth($y) != 12 ? 12 : -12; } } else { $m = -$m; } $rest -= $daysInMonth; $daysInMonth = LunarUtil::getDaysOfMonth($y, $m); } $d = $daysInMonth - $rest; } return new Lunar($y, $m, $d, $this->hour, $this->minute, $this->second); } public function toFullString() { $s = ''; $s .= $this; $s .= ' '; $s .= $this->getYearInGanZhi(); $s .= '('; $s .= $this->getYearShengXiao(); $s .= ')年 '; $s .= $this->getMonthInGanZhi(); $s .= '('; $s .= $this->getMonthShengXiao(); $s .= ')月 '; $s .= $this->getDayInGanZhi(); $s .= '('; $s .= $this->getDayShengXiao(); $s .= ')日 '; $s .= $this->getTimeZhi(); $s .= '('; $s .= $this->getTimeShengXiao(); $s .= ')时 纳音['; $s .= $this->getYearNaYin(); $s .= ' '; $s .= $this->getMonthNaYin(); $s .= ' '; $s .= $this->getDayNaYin(); $s .= ' '; $s .= $this->getTimeNaYin(); $s .= '] 星期'; $s .= $this->getWeekInChinese(); foreach ($this->getFestivals() as $f) { $s .= ' (' . $f . ')'; } foreach ($this->getOtherFestivals() as $f) { $s .= ' (' . $f . ')'; } $jq = $this->getJieQi(); if (strlen($jq) > 0) { $s .= ' (' . $jq . ')'; } $s .= ' '; $s .= $this->getGong(); $s .= '方'; $s .= $this->getShou(); $s .= ' 星宿['; $s .= $this->getXiu(); $s .= $this->getZheng(); $s .= $this->getAnimal(); $s .= ']('; $s .= $this->getXiuLuck(); $s .= ') 彭祖百忌['; $s .= $this->getPengZuGan(); $s .= ' '; $s .= $this->getPengZuZhi(); $s .= '] 喜神方位['; $s .= $this->getDayPositionXi(); $s .= ']('; $s .= $this->getDayPositionXiDesc(); $s .= ') 阳贵神方位['; $s .= $this->getDayPositionYangGui(); $s .= ']('; $s .= $this->getDayPositionYangGuiDesc(); $s .= ') 阴贵神方位['; $s .= $this->getDayPositionYinGui(); $s .= ']('; $s .= $this->getDayPositionYinGuiDesc(); $s .= ') 福神方位['; $s .= $this->getDayPositionFu(); $s .= ']('; $s .= $this->getDayPositionFuDesc(); $s .= ') 财神方位['; $s .= $this->getDayPositionCai(); $s .= ']('; $s .= $this->getDayPositionCaiDesc(); $s .= ') 冲['; $s .= $this->getChongDesc(); $s .= '] 煞['; $s .= $this->getSha(); $s .= ']'; return $s; } public function toString() { return $this->getYearInChinese() . '年' . $this->getMonthInChinese() . '月' . $this->getDayInChinese(); } public function __toString() { return $this->toString(); } /** * 获取年所在旬(以正月初一作为新年的开始) * @return string 旬 */ public function getYearXun() { return LunarUtil::getXun($this->getYearInGanZhi()); } /** * 获取年所在旬(以立春当天作为新年的开始) * @return string 旬 */ public function getYearXunByLiChun() { return LunarUtil::getXun($this->getYearInGanZhiByLiChun()); } /** * 获取年所在旬(以立春交接时刻作为新年的开始) * @return string 旬 */ public function getYearXunExact() { return LunarUtil::getXun($this->getYearInGanZhiExact()); } /** * 获取值年空亡(以正月初一作为新年的开始) * @return string 空亡(旬空) */ public function getYearXunKong() { return LunarUtil::getXunKong($this->getYearInGanZhi()); } /** * 获取值年空亡(以立春当天作为新年的开始) * @return string 空亡(旬空) */ public function getYearXunKongByLiChun() { return LunarUtil::getXunKong($this->getYearInGanZhiByLiChun()); } /** * 获取值年空亡(以立春交接时刻作为新年的开始) * @return string 空亡(旬空) */ public function getYearXunKongExact() { return LunarUtil::getXunKong($this->getYearInGanZhiExact()); } /** * 获取月所在旬(以节交接当天起算) * @return string 旬 */ public function getMonthXun() { return LunarUtil::getXun($this->getMonthInGanZhi()); } /** * 获取月所在旬(以节交接时刻起算) * @return string 旬 */ public function getMonthXunExact() { return LunarUtil::getXun($this->getMonthInGanZhiExact()); } /** * 获取值月空亡(以节交接当天起算) * @return string 空亡(旬空) */ public function getMonthXunKong() { return LunarUtil::getXunKong($this->getMonthInGanZhi()); } /** * 获取值月空亡(以节交接时刻起算) * @return string 空亡(旬空) */ public function getMonthXunKongExact() { return LunarUtil::getXunKong($this->getMonthInGanZhiExact()); } /** * 获取日所在旬(以节交接当天起算) * @return string 旬 */ public function getDayXun() { return LunarUtil::getXun($this->getDayInGanZhi()); } /** * 获取日所在旬(八字流派1,晚子时日柱算明天) * @return string 旬 */ public function getDayXunExact() { return LunarUtil::getXun($this->getDayInGanZhiExact()); } /** * 获取日所在旬(八字流派2,晚子时日柱算当天) * @return string 旬 */ public function getDayXunExact2() { return LunarUtil::getXun($this->getDayInGanZhiExact2()); } /** * 获取值日空亡 * @return string 空亡(旬空) */ public function getDayXunKong() { return LunarUtil::getXunKong($this->getDayInGanZhi()); } /** * 获取值日空亡(八字流派1,晚子时日柱算明天) * @return string 空亡(旬空) */ public function getDayXunKongExact() { return LunarUtil::getXunKong($this->getDayInGanZhiExact()); } /** * 获取值日空亡(八字流派2,晚子时日柱算当天) * @return string 空亡(旬空) */ public function getDayXunKongExact2() { return LunarUtil::getXunKong($this->getDayInGanZhiExact2()); } /** * 获取时辰所在旬 * @return string 旬 */ public function getTimeXun() { return LunarUtil::getXun($this->getTimeInGanZhi()); } /** * 获取值时空亡 * @return string 空亡(旬空) */ public function getTimeXunKong() { return LunarUtil::getXunKong($this->getTimeInGanZhi()); } public function getShuJiu() { try { $currentCalendar = new DateTime($this->solar->getYear() . '-' . $this->solar->getMonth() . '-' . $this->solar->getDay() . ' 0:0:0'); } catch (Exception $e) { return null; } $start = $this->jieQi[Lunar::$JIE_QI_APPEND]; try { $startCalendar = new DateTime($start->getYear() . '-' . $start->getMonth() . '-' . $start->getDay() . ' 0:0:0'); } catch (Exception $e) { return null; } if ($currentCalendar < $startCalendar) { $start = $this->jieQi[Lunar::$JIE_QI_FIRST]; try { $startCalendar = new DateTime($start->getYear() . '-' . $start->getMonth() . '-' . $start->getDay() . ' 0:0:0'); } catch (Exception $e) { return null; } } try { $endCalendar = new DateTime($start->getYear() . '-' . $start->getMonth() . '-' . $start->getDay() . ' 0:0:0'); } catch (Exception $e) { return null; } $endCalendar->modify('+81 day'); if ($currentCalendar < $startCalendar || $currentCalendar >= $endCalendar) { return null; } $days = intval(floor(($currentCalendar->getTimestamp() - $startCalendar->getTimestamp()) / (60 * 60 * 24))); return new ShuJiu(LunarUtil::$NUMBER[$days / 9 + 1] . '九', $days % 9 + 1); } public function getFu() { try { $currentCalendar = new DateTime($this->solar->getYear() . '-' . $this->solar->getMonth() . '-' . $this->solar->getDay() . ' 0:0:0'); } catch (Exception $e) { return null; } $xiaZhi = $this->jieQi['夏至']; $liQiu = $this->jieQi['立秋']; try { $startCalendar = new DateTime($xiaZhi->getYear() . '-' . $xiaZhi->getMonth() . '-' . $xiaZhi->getDay() . ' 0:0:0'); } catch (Exception $e) { return null; } $add = 6 - $xiaZhi->getLunar()->getDayGanIndex(); if ($add < 0) { $add += 10; } $add += 20; $startCalendar->modify('+' . $add . ' day'); if ($currentCalendar < $startCalendar) { return null; } $seconds = 60 * 60 * 24; $days = intval(floor(($currentCalendar->getTimestamp() - $startCalendar->getTimestamp()) / $seconds)); if ($days < 10) { return new Fu('初伏', $days + 1); } $startCalendar->modify('+10 day'); $days = intval(floor(($currentCalendar->getTimestamp() - $startCalendar->getTimestamp()) / $seconds)); if ($days < 10) { return new Fu('中伏', $days + 1); } $startCalendar->modify('+10 day'); $days = intval(floor(($currentCalendar->getTimestamp() - $startCalendar->getTimestamp()) / $seconds)); try { $liQiuCalendar = new DateTime($liQiu->getYear() . '-' . $liQiu->getMonth() . '-' . $liQiu->getDay() . ' 0:0:0'); } catch (Exception $e) { return null; } if ($liQiuCalendar <= $startCalendar) { if ($days < 10) { return new Fu('末伏', $days + 1); } } else { if ($days < 10) { return new Fu('中伏', $days + 11); } $startCalendar->modify('+10 day'); $days = intval(floor(($currentCalendar->getTimestamp() - $startCalendar->getTimestamp()) / $seconds)); if ($days < 10) { return new Fu('末伏', $days + 1); } } return null; } } src/NineStar.php000064400000016731146412202250007575 0ustar00index = $index; } /** * 获取九数 * @return string 九数 */ public function getNumber() { return NineStar::$NUMBER[$this->index]; } /** * 获取七色 * @return string 七色 */ public function getColor() { return NineStar::$COLOR[$this->index]; } /** * 获取五行 * @return string 五行 */ public function getWuXing() { return NineStar::$WU_XING[$this->index]; } /** * 获取方位 * @return string 方位 */ public function getPosition() { return NineStar::$POSITION[$this->index]; } /** * 获取方位描述 * @return string 方位描述 */ public function getPositionDesc() { return LunarUtil::$POSITION_DESC [$this->getPosition()]; } /** * 获取玄空九星名称 * @return string 玄空九星名称 */ public function getNameInXuanKong() { return NineStar::$NAME_XUAN_KONG[$this->index]; } /** * 获取北斗九星名称 * @return string 北斗九星名称 */ public function getNameInBeiDou() { return NineStar::$NAME_BEI_DOU[$this->index]; } /** * 获取奇门九星名称 * @return string 奇门九星名称 */ public function getNameInQiMen() { return NineStar::$NAME_QI_MEN[$this->index]; } /** * 获取太乙九神名称 * @return string 太乙九神名称 */ public function getNameInTaiYi() { return NineStar::$NAME_TAI_YI[$this->index]; } /** * 获取奇门九星吉凶 * @return string 大吉/小吉/大凶/小凶 */ public function getLuckInQiMen() { return NineStar::$LUCK_QI_MEN[$this->index]; } /** * 获取玄空九星吉凶 * @return string 吉/凶 */ public function getLuckInXuanKong() { return NineStar::$LUCK_XUAN_KONG[$this->index]; } /** * 获取奇门九星阴阳 * @return string 阴/阳 */ public function getYinYangInQiMen() { return NineStar::$YIN_YANG_QI_MEN[$this->index]; } /** * 获取太乙九神类型 * @return string 吉神/凶神/安神 */ public function getTypeInTaiYi() { return NineStar::$TYPE_TAI_YI[$this->index]; } /** * 获取八门(奇门遁甲) * @return string 八门 */ public function getBaMenInQiMen() { return NineStar::$BA_MEN_QI_MEN[$this->index]; } /** * 获取太乙九神歌诀 * @return string 太乙九神歌诀 */ public function getSongInTaiYi() { return NineStar::$SONG_TAI_YI[$this->index]; } /** * 获取九星序号,从0开始 * @return int 序号 */ public function getIndex() { return $this->index; } public function toString() { return $this->getNumber() . $this->getColor() . $this->getWuXing() . $this->getNameInBeiDou(); } public function __toString() { return $this->toString(); } public function toFullString() { $s = ''; $s .= $this->getNumber(); $s .= $this->getColor(); $s .= $this->getWuXing(); $s .= ' '; $s .= $this->getPosition(); $s .= '('; $s .= $this->getPositionDesc(); $s .= ') '; $s .= $this->getNameInBeiDou(); $s .= ' 玄空['; $s .= $this->getNameInXuanKong(); $s .= ' '; $s .= $this->getLuckInXuanKong(); $s .= '] 奇门['; $s .= $this->getNameInQiMen(); $s .= ' '; $s .= $this->getLuckInQiMen(); if (strlen($this->getBaMenInQiMen()) > 0) { $s .= ' '; $s .= $this->getBaMenInQiMen(); $s .= '门'; } $s .= ' '; $s .= $this->getYinYangInQiMen(); $s .= '] 太乙['; $s .= $this->getNameInTaiYi(); $s .= ' '; $s .= $this->getTypeInTaiYi(); $s .= ']'; return $s; } } src/ShuJiu.php000064400000001523146412202250007252 0ustar00name = $name; $this->index = $index; } public function getName() { return $this->name; } public function setName($name) { $this->name = $name; } public function getIndex() { return $this->index; } public function setIndex($index) { $this->index = $index; } public function toString() { return $this->name; } public function __toString() { return $this->toString(); } public function toFullString() { return $this->name . '第' . $this->index . '天'; } } src/Solar.php000064400000026355146412202250007135 0ustar00year = $year; $this->month = $month; $this->day = $day; $this->hour = $hour; $this->minute = $minute; $this->second = $second; try { $this->calendar = new DateTime($year . '-' . $month . '-' . $day . ' ' . $hour . ':' . $minute . ':' . $second); } catch (Exception $e) { } } public static function fromDate($date) { $year = (int)date_format($date, 'Y'); $month = (int)date_format($date, 'n'); $day = (int)date_format($date, 'j'); $hour = (int)date_format($date, 'G'); $minute = (int)date_format($date, 'i'); $second = (int)date_format($date, 's'); return new Solar($year, $month, $day, $hour, $minute, $second); } public static function fromJulianDay($julianDay) { $d = intval(floor($julianDay + 0.5)); $f = $julianDay + 0.5 - $d; if ($d >= 2299161) { $c = intval(floor(($d - 1867216.25) / 36524.25)); $d += 1 + $c - intval(floor($c / 4)); } $d += 1524; $year = intval(floor(($d - 122.1) / 365.25)); $d -= intval(floor(365.25 * $year)); $month = intval(floor($d / 30.601)); $d -= intval(floor(30.601 * $month)); $day = $d; if ($month > 13) { $month -= 13; $year -= 4715; } else { $month -= 1; $year -= 4716; } $f *= 24; $hour = intval(floor($f)); $f -= $hour; $f *= 60; $minute = intval(floor($f)); $f -= $minute; $f *= 60; $second = intval(round($f)); return Solar::fromYmdHms($year, $month, $day, $hour, $minute, $second); } /** * 八字转阳历,默认流派 * @param string $yearGanZhi 年柱 * @param string $monthGanZhi 月柱 * @param string $dayGanZhi 日柱 * @param string $timeGanZhi 时柱 * @return Solar[] * @throws Exception */ public static function fromBaZi($yearGanZhi, $monthGanZhi, $dayGanZhi, $timeGanZhi) { return self::fromBaZiBySect($yearGanZhi, $monthGanZhi, $dayGanZhi, $timeGanZhi, 2); } /** * 八字转阳历 * @param string $yearGanZhi 年柱 * @param string $monthGanZhi 月柱 * @param string $dayGanZhi 日柱 * @param string $timeGanZhi 时柱 * @param int sect 流派,2晚子时日柱按当天,1晚子时日柱按明天 * @return Solar[] * @throws Exception */ public static function fromBaZiBySect($yearGanZhi, $monthGanZhi, $dayGanZhi, $timeGanZhi, $sect) { $sect = (1 == $sect) ? 1 : 2; $l = array(); $today = Solar::fromDate(new DateTime()); $lunar = $today->getLunar(); $offsetYear = LunarUtil::getJiaZiIndex($lunar->getYearInGanZhiExact()) - LunarUtil::getJiaZiIndex($yearGanZhi); if ($offsetYear < 0) { $offsetYear = $offsetYear + 60; } $startYear = $today->getYear() - $offsetYear; $hour = 0; $timeZhi = substr($timeGanZhi, strlen($timeGanZhi) / 2); for ($i = 0, $j = count(LunarUtil::$ZHI); $i < $j; $i++) { if (strcmp(LunarUtil::$ZHI[$i], $timeZhi) == 0) { $hour = ($i - 1) * 2; } } while ($startYear >= SolarUtil::$BASE_YEAR - 1) { $year = $startYear - 1; $counter = 0; $month = 12; $found = false; while ($counter < 15) { if ($year >= SolarUtil::$BASE_YEAR) { $day = 1; if ($year == SolarUtil::$BASE_YEAR && $month == SolarUtil::$BASE_MONTH) { $day = SolarUtil::$BASE_DAY; } $solar = Solar::fromYmdHms($year, $month, $day, $hour, 0, 0); $lunar = $solar->getLunar(); if (strcmp($lunar->getYearInGanZhiExact(), $yearGanZhi) == 0 && strcmp($lunar->getMonthInGanZhiExact(), $monthGanZhi) == 0) { $found = true; break; } } $month++; if ($month > 12) { $month = 1; $year++; } $counter++; } if ($found) { $counter = 0; $month--; if ($month < 1) { $month = 12; $year--; } $day = 1; if ($year == SolarUtil::$BASE_YEAR && $month == SolarUtil::$BASE_MONTH) { $day = SolarUtil::$BASE_DAY; } $solar = Solar::fromYmdHms($year, $month, $day, $hour, 0, 0); while ($counter < 61) { $lunar = $solar->getLunar(); $dgz = (2 == $sect) ? $lunar->getDayInGanZhiExact2() : $lunar->getDayInGanZhiExact(); if (strcmp($lunar->getYearInGanZhiExact(), $yearGanZhi) == 0 && strcmp($lunar->getMonthInGanZhiExact(), $monthGanZhi) == 0 && strcmp($dgz, $dayGanZhi) == 0 && strcmp($lunar->getTimeInGanZhi(), $timeGanZhi) == 0) { $l[] = $solar; break; } $solar = $solar->next(1); $counter++; } } $startYear -= 60; } return $l; } public static function fromYmd($year, $month, $day) { return new Solar($year, $month, $day, 0, 0, 0); } public static function fromYmdHms($year, $month, $day, $hour, $minute, $second) { return new Solar($year, $month, $day, $hour, $minute, $second); } public function toYmd() { $month = $this->month; $day = $this->day; return $this->year . '-' . ($month < 10 ? '0' : '') . $month . '-' . ($day < 10 ? '0' : '') . $day; } public function toYmdHms() { $hour = $this->hour; $minute = $this->minute; $second = $this->second; return $this->toYmd() . ' ' . ($hour < 10 ? '0' : '') . $hour . ':' . ($minute < 10 ? '0' : '') . $minute . ':' . ($second < 10 ? '0' : '') . $second; } public function toFullString() { $s = $this->toYmdHms(); if ($this->isLeapYear()) { $s .= ' 闰年'; } $s .= ' 星期' . $this->getWeekInChinese(); foreach ($this->getFestivals() as $f) { $s .= ' (' . $f . ')'; } $s .= ' ' . $this->getXingZuo() . '座'; return $s; } public function getYear() { return $this->year; } public function getMonth() { return $this->month; } public function getDay() { return $this->day; } public function getHour() { return $this->hour; } public function getMinute() { return $this->minute; } public function getSecond() { return $this->second; } public function getJulianDay() { $y = $this->year; $m = $this->month; $d = $this->day + (($this->second / 60 + $this->minute) / 60 + $this->hour) / 24; $n = 0; $g = false; if ($y * 372 + $m * 31 + intval(floor($d)) >= 588829) { $g = true; } if ($m <= 2) { $m += 12; $y--; } if ($g) { $n = intval(floor($y / 100)); $n = 2 - $n + intval(floor($n / 4)); } return intval(floor(365.25 * ($y + 4716))) + intval(floor(30.6001 * ($m + 1))) + $d + $n - 1524.5; } public function getLunar() { return Lunar::fromDate($this->calendar); } public function getCalendar() { return $this->calendar; } public function toString() { return $this->toYmd(); } public function __toString() { return $this->toString(); } public function isLeapYear() { return SolarUtil::isLeapYear($this->year); } public function getWeek() { return (int)$this->calendar->format('w'); } public function getWeekInChinese() { return SolarUtil::$WEEK[$this->getWeek()]; } public function getXingZuo() { $index = 11; $y = $this->month * 100 + $this->day; if ($y >= 321 && $y <= 419) { $index = 0; } else if ($y >= 420 && $y <= 520) { $index = 1; } else if ($y >= 521 && $y <= 621) { $index = 2; } else if ($y >= 622 && $y <= 722) { $index = 3; } else if ($y >= 723 && $y <= 822) { $index = 4; } else if ($y >= 823 && $y <= 922) { $index = 5; } else if ($y >= 923 && $y <= 1023) { $index = 6; } else if ($y >= 1024 && $y <= 1122) { $index = 7; } else if ($y >= 1123 && $y <= 1221) { $index = 8; } else if ($y >= 1222 || $y <= 119) { $index = 9; } else if ($y <= 218) { $index = 10; } return SolarUtil::$XING_ZUO[$index]; } public function getFestivals() { $l = array(); if (!empty(SolarUtil::$FESTIVAL[$this->month . '-' . $this->day])) { $l[] = SolarUtil::$FESTIVAL[$this->month . '-' . $this->day]; } $weeks = intval(ceil($this->day / 7.0)); $week = $this->getWeek(); if (!empty(SolarUtil::$WEEK_FESTIVAL[$this->month . '-' . $weeks . '-' . $week])) { $l[] = SolarUtil::$WEEK_FESTIVAL[$this->month . '-' . $weeks . '-' . $week]; } return $l; } public function getOtherFestivals() { $l = array(); if (!empty(SolarUtil::$OTHER_FESTIVAL[$this->month . '-' . $this->day])) { $l[] = SolarUtil::$OTHER_FESTIVAL[$this->month . '-' . $this->day]; } return $l; } /** * 获取往后推几天的阳历日期,如果要往前推,则天数用负数 * @param int $days 天数 * @return Solar|null */ public function next($days) { if ($days == 0) { return Solar::fromYmdHms($this->year, $this->month, $this->day, $this->hour, $this->minute, $this->second); } try { $calendar = new DateTime($this->year . '-' . $this->month . '-' . $this->day . ' ' . $this->hour . ':' . $this->minute . ':' . $this->second); } catch (Exception $e) { return null; } $calendar->modify(($days > 0 ? '+' : '') . $days . ' day'); return Solar::fromDate($calendar); } /** * 获取往后推几个工作日的阳历日期,如果要往前推,则天数用负数 * @param int $days 天数 * @return Solar|null */ public function nextWorkday($days) { if ($days == 0) { return Solar::fromYmdHms($this->year, $this->month, $this->day, $this->hour, $this->minute, $this->second); } try { $calendar = new DateTime($this->year . '-' . $this->month . '-' . $this->day . ' ' . $this->hour . ':' . $this->minute . ':' . $this->second); } catch (Exception $e) { return null; } $rest = abs($days); $add = $days < 1 ? -1 : 1; while ($rest > 0) { $calendar->modify(($add > 0 ? '+' : '') . $add . ' day'); $work = true; $year = (int)date_format($calendar, 'Y'); $month = (int)date_format($calendar, 'n'); $day = (int)date_format($calendar, 'j'); $holiday = HolidayUtil::getHolidayByYmd($year, $month, $day); if (null == $holiday) { $week = (int)$calendar->format('w'); if (0 == $week || 6 == $week) { $work = false; } } else { $work = $holiday->isWork(); } if ($work) { $rest -= 1; } } return Solar::fromDate($calendar); } } src/SolarHalfYear.php000064400000004635146412202250010546 0ustar00year = $year; $this->month = $month; } public function toString() { return $this->year . '.' . $this->getIndex(); } public function __toString() { return $this->toString(); } public function toFullString() { return $this->year . '年' . (1 === $this->getIndex() ? '上' : '下') . '半年'; } /** * 通过指定年月获取阳历半年 * @param int $year 年 * @param int $month 月,1到12 * @return SolarHalfYear */ public static function fromYm($year, $month) { return new SolarHalfYear($year, $month); } /** * 通过指定日期获取阳历半年 * @param DateTime $date 日期DateTime * @return SolarHalfYear */ public static function fromDate($date) { $year = (int)date_format($date, 'Y'); $month = (int)date_format($date, 'n'); return new SolarHalfYear($year, $month); } public function getYear() { return $this->year; } public function getMonth() { return $this->month; } /** * 获取当月是第几半年,从1开始 * @return int */ public function getIndex() { return (int)ceil($this->month / SolarHalfYear::$MONTH_COUNT); } /** * 获取本半年的月份 * @return array */ public function getMonths() { $l = array(); $index = $this->getIndex() - 1; for ($i = 0; $i < SolarHalfYear::$MONTH_COUNT; $i++) { $l[] = new SolarHalfYear($this->year, SolarHalfYear::$MONTH_COUNT * $index + $i + 1); } return $l; } /** * 半年推移 * @param int $halfYears 推移的半年数,负数为倒推 * @return SolarHalfYear|null */ public function next($halfYears) { if (0 === $halfYears) { return new SolarHalfYear($this->year, $this->month); } try { $date = new DateTime($this->year . '-' . $this->month . '-1'); } catch (Exception $e) { return null; } $date->modify((SolarHalfYear::$MONTH_COUNT * $halfYears) . ' month'); return SolarHalfYear::fromDate($date); } } src/SolarMonth.php000064400000004047146412202250010135 0ustar00year = $year; $this->month = $month; } public function toString() { return $this->year . '-' . $this->month; } public function __toString() { return $this->toString(); } public function toFullString() { return $this->year . '年' . $this->month . '月'; } /** * 通过指定年月获取阳历月 * @param int $year 年 * @param int $month 月,1到12 * @return SolarMonth */ public static function fromYm($year, $month) { return new SolarMonth($year, $month); } /** * 通过指定日期获取阳历月 * @param DateTime $date 日期DateTime * @return SolarMonth */ public static function fromDate($date) { $year = (int)date_format($date, 'Y'); $month = (int)date_format($date, 'n'); return new SolarMonth($year, $month); } public function getYear() { return $this->year; } public function getMonth() { return $this->month; } /** * 获取本月的阳历日期列表 * @return array */ public function getDays() { $l = array(); $d = Solar::fromYmd($this->year, $this->month, 1); $l[] = $d; $days = SolarUtil::getDaysOfMonth($this->year, $this->month); for ($i = 1; $i < $days; $i++) { $l[] = $d->next($i); } return $l; } /** * 获取往后推几个月的阳历月,如果要往前推,则月数用负数 * @param int $months 月数 * @return SolarMonth|null */ public function next($months) { try { $date = new DateTime($this->year . '-' . $this->month . '-1'); } catch (Exception $e) { return null; } $date->modify($months . ' month'); return SolarMonth::fromDate($date); } } src/SolarSeason.php000064400000004543146412202250010301 0ustar00year = $year; $this->month = $month; } public function toString() { return $this->year . '.' . $this->getIndex(); } public function __toString() { return $this->toString(); } public function toFullString() { return $this->year . '年' . $this->getIndex() . '季度'; } /** * 通过指定年月获取阳历季度 * @param int $year 年 * @param int $month 月,1到12 * @return SolarSeason */ public static function fromYm($year, $month) { return new SolarSeason($year, $month); } /** * 通过指定日期获取阳历季度 * @param DateTime $date 日期DateTime * @return SolarSeason */ public static function fromDate($date) { $year = (int)date_format($date, 'Y'); $month = (int)date_format($date, 'n'); return new SolarSeason($year, $month); } public function getYear() { return $this->year; } public function getMonth() { return $this->month; } /** * 获取当月是第几季度,从1开始 * @return int */ public function getIndex() { return (int)ceil($this->month / SolarSeason::$MONTH_COUNT); } /** * 获取本季度的月份 * @return array */ public function getMonths() { $l = array(); $index = $this->getIndex() - 1; for ($i = 0; $i < SolarSeason::$MONTH_COUNT; $i++) { $l[] = new SolarMonth($this->year, SolarSeason::$MONTH_COUNT * $index + $i + 1); } return $l; } /** * 季度推移 * @param int $seasons 推移的季度数,负数为倒推 * @return SolarSeason|null */ public function next($seasons) { if (0 === $seasons) { return new SolarSeason($this->year, $this->month); } try { $date = new DateTime($this->year . '-' . $this->month . '-1'); } catch (Exception $e) { return null; } $date->modify((SolarSeason::$MONTH_COUNT * $seasons) . ' month'); return SolarSeason::fromDate($date); } } src/SolarWeek.php000064400000014372146412202250007745 0ustar00year = $year; $this->month = $month; $this->day = $day; $this->start = $start; } public function toString() { return $this->year . '.' . $this->month . '.' . $this->getIndex(); } public function __toString() { return $this->toString(); } public function toFullString() { return $this->year . '年' . $this->month . '月第' . $this->getIndex() . '周'; } /** * 通过指定年月日获取阳历周 * @param int $year 年 * @param int $month 月,1到12 * @param int $day 日,1到31 * @param int $start 星期几作为一周的开始,1234560分别代表星期一至星期天 * @return SolarWeek */ public static function fromYmd($year, $month, $day, $start) { return new SolarWeek($year, $month, $day, $start); } /** * 通过指定日期获取阳历周 * @param DateTime $date 日期DateTime * @param int $start 星期几作为一周的开始,1234560分别代表星期一至星期天 * @return SolarWeek */ public static function fromDate($date, $start) { $year = (int)date_format($date, 'Y'); $month = (int)date_format($date, 'n'); $day = (int)date_format($date, 'j'); return new SolarWeek($year, $month, $day, $start); } public function getYear() { return $this->year; } public function getMonth() { return $this->month; } public function getDay() { return $this->day; } public function getStart() { return $this->start; } /** * 获取当前日期是在当月第几周 * @return int */ public function getIndex() { $firstDayWeek = (int)date('w', strtotime($this->year . '-' . $this->month . '-1')); if ($firstDayWeek === 0) { $firstDayWeek = 7; } return ceil(($this->day + $firstDayWeek - $this->start) / 7); } /** * 周推移 * @param int $weeks 推移的周数,负数为倒推 * @param bool $separateMonth 是否按月单独计算 * @return SolarWeek|null */ public function next($weeks, $separateMonth) { if (0 === $weeks) { return SolarWeek::fromYmd($this->year, $this->month, $this->day, $this->start); } if ($separateMonth) { $n = $weeks; try { $date = new DateTime($this->year . '-' . $this->month . '-' . $this->day); } catch (Exception $e) { return null; } $week = SolarWeek::fromDate($date, $this->start); $month = $this->month; $plus = $n > 0; while (0 !== $n) { $date->modify(($plus ? 7 : -7) . ' day'); $week = SolarWeek::fromDate($date, $this->start); $weekMonth = $week->getMonth(); if ($month !== $weekMonth) { $index = $week->getIndex(); if ($plus) { if (1 === $index) { $firstDay = $week->getFirstDay(); $week = SolarWeek::fromYmd($firstDay->getYear(), $firstDay->getMonth(), $firstDay->getDay(), $this->start); $weekMonth = $week->getMonth(); } else { try { $date = new DateTime($week->year . '-' . $week->month . '-1'); } catch (Exception $e) { return null; } $week = SolarWeek::fromDate($date, $this->start); } } else { $size = SolarUtil::getWeeksOfMonth($week->getYear(), $week->getMonth(), $week->getStart()); if ($size === $index) { $lastDay = $week->getFirstDay()->next(6); $week = SolarWeek::fromYmd($lastDay->getYear(), $lastDay->getMonth(), $lastDay->getDay(), $this->start); $weekMonth = $week->getMonth(); } else { try { $date = new DateTime($this->year . '-' . $this->month . '-' . SolarUtil::getDaysOfMonth($week->getYear(), $week->getMonth())); } catch (Exception $e) { return null; } $week = SolarWeek::fromDate($date, $this->start); } } $month = $weekMonth; } $n -= $plus ? 1 : -1; } return $week; } else { try { $date = new DateTime($this->year . '-' . $this->month . '-' . $this->day); } catch (Exception $e) { return null; } $date->modify(($weeks * 7) . ' day'); return SolarWeek::fromDate($date, $this->start); } } /** * 获取本周第一天的阳历日期(可能跨月) * @return Solar|null */ public function getFirstDay() { try { $date = new DateTime($this->year . '-' . $this->month . '-' . $this->day); } catch (Exception $e) { return null; } $week = (int)$date->format('w'); $prev = $week - $this->start; if ($prev < 0) { $prev += 7; } $date->modify(-$prev . ' day'); return Solar::fromDate($date); } /** * 获取本周第一天的阳历日期(仅限当月) * @return Solar|null */ public function getFirstDayInMonth() { $days = $this->getDays(); foreach ($days as $day) { if ($this->month === $day->getMonth()) { return $day; } } return null; } /** * 获取本周的阳历日期列表(可能跨月) * @return array */ public function getDays() { $firstDay = $this->getFirstDay(); $l = array(); if (null == $firstDay) { return $l; } $l[] = $firstDay; for ($i = 1; $i < 7; $i++) { $l[] = $firstDay->next($i); } return $l; } /** * 获取本周的阳历日期列表(仅限当月) * @return array */ public function getDaysInMonth() { $days = $this->getDays(); $l = array(); foreach ($days as $day) { if ($this->month !== $day->getMonth()) { continue; } $l[] = $day; } return $l; } } src/SolarYear.php000064400000003364146412202250007751 0ustar00year = $year; } public function toString() { return $this->year . ''; } public function __toString() { return $this->toString(); } public function toFullString() { return $this->year . '年'; } /** * 通过指定年获取阳历年 * @param int $year 年 * @return SolarYear */ public static function fromYear($year) { return new SolarYear($year); } /** * 通过指定日期获取阳历年 * @param DateTime $date 日期DateTime * @return SolarYear */ public static function fromDate($date) { $year = (int)date_format($date, 'Y'); return new SolarYear($year); } public function getYear() { return $this->year; } /** * 获取本年的月份 * @return array */ public function getMonths() { $l = array(); $month = SolarMonth::fromYm($this->year, 1); $l[] = $month; for ($i = 1; $i < SolarYear::$MONTH_COUNT; $i++) { $l[] = $month->next($i); } return $l; } /** * 年推移 * @param int $years 推移的年数,负数为倒推 * @return SolarYear|null */ public function next($years) { if (0 === $years) { return new SolarYear($this->year); } try { $date = new DateTime($this->year . '-1-1'); } catch (Exception $e) { return null; } $date->modify($years . ' year'); return SolarYear::fromDate($date); } } src/XiaoYun.php000064400000004565146412202250007450 0ustar00daYun = $daYun; $this->lunar = $daYun->getLunar(); $this->index = $index; $this->year = $daYun->getStartYear() + $index; $this->age = $daYun->getStartAge() + $index; $this->forward = $forward; } /** * 获取序数 * @return int */ public function getIndex() { return $this->index; } /** * 获取大运 * @return DaYun */ public function getDaYun() { return $this->daYun; } /** * 获取年 * @return int */ public function getYear() { return $this->year; } /** * 获取年龄 * @return int */ public function getAge() { return $this->age; } /** * 获取阴历 * @return Lunar */ public function getLunar() { return $this->lunar; } /** * 是否顺推 * @return bool */ public function isForward() { return $this->forward; } /** * 获取干支 * @return string */ public function getGanZhi() { $offset = LunarUtil::getJiaZiIndex($this->lunar->getTimeInGanZhi()); $add = $this->index + 1; if ($this->daYun->getIndex() > 0) { $add += $this->daYun->getStartAge() - 1; } $offset += $this->forward ? $add : -$add; $size = count(LunarUtil::$JIA_ZI); while ($offset < 0) { $offset += $size; } $offset %= $size; return LunarUtil::$JIA_ZI[$offset]; } /** * 获取所在旬 * @return string 旬 */ public function getXun() { return LunarUtil::getXun($this->getGanZhi()); } /** * 获取旬空(空亡) * @return string 旬空(空亡) */ public function getXunKong() { return LunarUtil::getXunKong($this->getGanZhi()); } } src/Yun.php000064400000007502146412202250006621 0ustar00lunar = $eightChar->getLunar(); $this->gender = $gender; // 阳 $yang = 0 == $this->lunar->getYearGanIndexExact() % 2; // 男 $man = 1 == $gender; $this->forward = ($yang && $man) || (!$yang && !$man); $this->computeStart(); } private function computeStart() { // 上节 $prev = $this->lunar->getPrevJie(); // 下节 $next = $this->lunar->getNextJie(); // 出生日期 $current = $this->lunar->getSolar(); // 阳男阴女顺推,阴男阳女逆推 $start = $this->forward ? $current : $prev->getSolar(); $end = $this->forward ? $next->getSolar() : $current; // 时辰差 $hourDiff = LunarUtil::getTimeZhiIndex(substr($end->toYmdHms(), 11, 5)) - LunarUtil::getTimeZhiIndex(substr($start->toYmdHms(), 11, 5)); try { $endCalendar = new DateTime($end->getYear() . '-' . $end->getMonth() . '-' . $end->getDay()); } catch (Exception $e) { return null; } $endCalendar->setTime(0, 0, 0); try { $startCalendar = new DateTime($start->getYear() . '-' . $start->getMonth() . '-' . $start->getDay()); } catch (Exception $e) { return null; } $startCalendar->setTime(0, 0, 0); // 天数差 $dayDiff = intval(floor(($endCalendar->getTimestamp() - $startCalendar->getTimestamp()) / 86400)); if ($hourDiff < 0) { $hourDiff += 12; $dayDiff--; } $monthDiff = intval(floor($hourDiff * 10 / 30)); $month = $dayDiff * 4 + $monthDiff; $day = $hourDiff * 10 - $monthDiff * 30; $year = intval(floor($month / 12)); $month = $month - $year * 12; $this->startYear = $year; $this->startMonth = $month; $this->startDay = $day; } /** * 获取性别 * @return int */ public function getGender() { return $this->gender; } /** * 获取起运年数 * @return int */ public function getStartYear() { return $this->startYear; } /** * 获取起运月数 * @return int */ public function getStartMonth() { return $this->startMonth; } /** * 获取起运天数 * @return int */ public function getStartDay() { return $this->startDay; } /** * 是否顺推 * @return bool */ public function isForward() { return $this->forward; } /** * 获取阴历日期 * @return Lunar */ public function getLunar() { return $this->lunar; } /** * 获取起运的阳历日期 * @return Solar|null */ public function getStartSolar() { $birth = $this->lunar->getSolar(); try { $date = new DateTime($birth->getYear() . '-' . $birth->getMonth() . '-' . $birth->getDay()); } catch (Exception $e) { return null; } $date->modify($this->startYear . ' year'); $date->modify($this->startMonth . ' month'); $date->modify($this->startDay . ' day'); return Solar::fromDate($date); } /** * 获取大运 * @return DaYun[] */ public function getDaYun() { $n = 10; $l = array(); for ($i = 0; $i < $n; $i++) { $l[] = new DaYun($this, $i); } return $l; } } src/util/HolidayUtil.php000064400000045352146412202250011257 0ustar00 0) { $right = substr($right, $n); } while ((strpos($right, $key) !== 0) && strlen($right) >= HolidayUtil::$SIZE) { $right = substr($right, HolidayUtil::$SIZE); } return $right; } private static function findBackward($key) { $start = strrpos(HolidayUtil::$DATA, $key); if (!$start) { return null; } $left = substr(HolidayUtil::$DATA, 0, $start + strlen($key)); $size = strlen($left); $n = $size % HolidayUtil::$SIZE; if ($n > 0) { $left = substr($left, 0, $size - $n); } $size = strlen($left); while ((substr_compare($left, $key, -strlen($key)) !== 0) && $size >= HolidayUtil::$SIZE) { $left = substr($left, 0, $size - HolidayUtil::$SIZE); $size = strlen($left); } return $left; } private static function findHolidaysForward($key) { $l = array(); $s = HolidayUtil::findForward($key); if (null == $s) { return $l; } while (strpos($s, $key) === 0) { $l[] = HolidayUtil::buildHolidayForward($s); $s = substr($s, HolidayUtil::$SIZE); } return $l; } private static function findHolidaysBackward($key) { $l = array(); $s = HolidayUtil::findBackward($key); if (null == $s) { return $l; } while (substr_compare($s, $key, -strlen($key)) === 0) { $l[] = HolidayUtil::buildHolidayBackward($s); $s = substr($s, 0, strlen($s) - HolidayUtil::$SIZE); } return array_reverse($l); } /** * 获取指定年月日的节假日信息,如果不存在,返回null * @param int $year 年 * @param int $month 月,1-12 * @param int $day 日,1-31 * @return Holiday|null */ public static function getHolidayByYmd($year, $month, $day) { $l = HolidayUtil::findHolidaysForward($year . HolidayUtil::padding($month) . HolidayUtil::padding($day)); return empty($l) ? null : $l[0]; } /** * 获取指定年月日的节假日信息,如果不存在,返回null * @param string $ymd 年月日 * @return Holiday|null */ public static function getHoliday($ymd) { $l = HolidayUtil::findHolidaysForward(str_replace('-', '', $ymd)); return empty($l) ? null : $l[0]; } /** * 获取指定年月的节假日列表 * @param int $year 年 * @param int $month 月,1-12 * @return array */ public static function getHolidaysByYm($year, $month) { return HolidayUtil::findHolidaysForward($year . HolidayUtil::padding($month)); } /** * 获取指定年的节假日列表 * @param int $year 年 * @return array */ public static function getHolidaysByYear($year) { return HolidayUtil::findHolidaysForward($year . ''); } /** * 获取指定年月日的节假日列表 * @param string $ymd 年、年月、年月日 * @return array 节假日列表 */ public static function getHolidays($ymd) { return HolidayUtil::findHolidaysForward(str_replace('-', '', $ymd)); } /** * 获取指定节日(仅节日当天)的相关节假日列表 * @param int $year 年 * @param int $month 月,1-12 * @param int $day 日,1-31 * @return array */ public static function getHolidaysByTargetYmd($year, $month, $day) { return HolidayUtil::findHolidaysBackward($year . HolidayUtil::padding($month) . HolidayUtil::padding($day)); } /** * 获取指定节日(仅节日当天)的相关节假日列表 * @param string $ymd 年月日 * @return array */ public static function getHolidaysByTarget($ymd) { return HolidayUtil::findHolidaysBackward(str_replace('-', '', $ymd)); } /** * 修正或追加节假日数据。节假日名称下标从0开始,超过9的,按ASCII码表依次往后排列;调休标识0为上班,否则放假 * @param array $names 用于替换默认的节假日名称列表,传null即可使用默认名称 * @param string $data 需要修正或追加的节假日数据,每18位表示1天依次排列,格式:当天年月日YYYYMMDD(8位)+节假日名称下标(1位)+调休标识(1位)+节假日当天YYYYMMDD(8位)。例:202005023120200501代表2020-05-02为劳动节放假,对应节假日为2020-05-01 */ public static function fix($names, $data) { if (null != $names) { self::$NAMES = $names; } if (null == $data) { return; } $append = ''; while (strlen($data) >= self::$SIZE) { $segment = substr($data, 0, self::$SIZE); $day = substr($segment, 0, 8); $holiday = self::getHoliday($day); if (null == $holiday) { $append .= $segment; } else { $nameIndex = -1; for ($i = 0, $j = count(self::$NAMES); $i < $j; $i++) { if (strcmp(self::$NAMES[$i], $holiday->getName()) == 0) { $nameIndex = $i; break; } } if ($nameIndex > -1) { $old = $day . chr($nameIndex + self::$ZERO) . ($holiday->isWork() ? '0' : '1') . str_replace('-', '', $holiday->getTarget()); self::$DATA = str_replace($old, $segment, self::$DATA); } } $data = substr($data, self::$SIZE); } if (strlen($append) > 0) { self::$DATA .= $append; } } }src/util/LunarUtil.php000064400000365656146412202250010763 0ustar00 4, '丑' => 2, '寅' => 0, '卯' => 10, '辰' => 8, '巳' => 6, '午' => 4, '未' => 2, '申' => 0, '酉' => 10, '戌' => 8, '亥' => 6 ); public static $TIAN_SHEN_TYPE = array( '青龙' => '黄道', '明堂' => '黄道', '金匮' => '黄道', '天德' => '黄道', '玉堂' => '黄道', '司命' => '黄道', '天刑' => '黑道', '朱雀' => '黑道', '白虎' => '黑道', '天牢' => '黑道', '玄武' => '黑道', '勾陈' => '黑道' ); public static $TIAN_SHEN_TYPE_LUCK = array( '黄道' => '吉', '黑道' => '凶' ); public static $PENG_ZU_GAN = array('', '甲不开仓财物耗散', '乙不栽植千株不长', '丙不修灶必见灾殃', '丁不剃头头必生疮', '戊不受田田主不祥', '己不破券二比并亡', '庚不经络织机虚张', '辛不合酱主人不尝', '壬不泱水更难提防', '癸不词讼理弱敌强'); public static $PENG_ZU_ZHI = array('', '子不问卜自惹祸殃', '丑不冠带主不还乡', '寅不祭祀神鬼不尝', '卯不穿井水泉不香', '辰不哭泣必主重丧', '巳不远行财物伏藏', '午不苫盖屋主更张', '未不服药毒气入肠', '申不安床鬼祟入房', '酉不会客醉坐颠狂', '戌不吃犬作怪上床', '亥不嫁娶不利新郎'); public static $NUMBER = array('〇', '一', '二', '三', '四', '五', '六', '七', '八', '九'); public static $MONTH = array('', '正', '二', '三', '四', '五', '六', '七', '八', '九', '十', '冬', '腊'); public static $SEASON = array('', '孟春', '仲春', '季春', '孟夏', '仲夏', '季夏', '孟秋', '仲秋', '季秋', '孟冬', '仲冬', '季冬'); public static $SHENG_XIAO = array('', '鼠', '牛', '虎', '兔', '龙', '蛇', '马', '羊', '猴', '鸡', '狗', '猪'); public static $QI = array('大寒', '雨水', '春分', '谷雨', '小满', '夏至', '大暑', '处暑', '秋分', '霜降', '小雪', '冬至'); public static $JIE = array('小寒', '立春', '惊蛰', '清明', '立夏', '芒种', '小暑', '立秋', '白露', '寒露', '立冬', '大雪'); public static $DAY = array('', '初一', '初二', '初三', '初四', '初五', '初六', '初七', '初八', '初九', '初十', '十一', '十二', '十三', '十四', '十五', '十六', '十七', '十八', '十九', '二十', '廿一', '廿二', '廿三', '廿四', '廿五', '廿六', '廿七', '廿八', '廿九', '三十'); public static $YUE_XIANG = array('', '朔', '既朔', '蛾眉新', '蛾眉新', '蛾眉', '夕月', '上弦', '上弦', '九夜', '宵', '宵', '宵', '渐盈凸', '小望', '望', '既望', '立待', '居待', '寝待', '更待', '渐亏凸', '下弦', '下弦', '有明', '有明', '蛾眉残', '蛾眉残', '残', '晓', '晦'); /** * 农历日期对应的节日 * @var array */ public static $FESTIVAL = array( '1-1' => '春节', '1-15' => '元宵节', '2-2' => '龙头节', '5-5' => '端午节', '7-7' => '七夕节', '8-15' => '中秋节', '9-9' => '重阳节', '12-8' => '腊八节', '12-30' => '除夕' ); /** * 农历日期对应的非正式节日 * @var array */ public static $OTHER_FESTIVAL = array( '1-1' => array('弥勒佛圣诞'), '1-8' => array('五殿阎罗天子诞'), '1-9' => array('玉皇上帝诞'), '2-1' => array('一殿秦广王诞'), '2-2' => array('福德土地正神诞'), '2-3' => array('文昌帝君诞'), '2-6' => array('东华帝君诞'), '2-8' => array('释迦牟尼佛出家'), '2-15' => array('释迦牟尼佛般涅槃'), '2-17' => array('东方杜将军诞'), '2-18' => array('至圣先师孔子讳辰'), '2-19' => array('观音大士诞'), '2-21' => array('普贤菩萨诞'), '3-1' => array('二殿楚江王诞'), '3-3' => array('玄天上帝诞'), '3-8' => array('六殿卞城王诞'), '3-15' => array('昊天上帝诞'), '3-16' => array('准提菩萨诞'), '3-19' => array('中岳大帝诞'), '3-20' => array('子孙娘娘诞'), '3-27' => array('七殿泰山王诞'), '3-28' => array('苍颉至圣先师诞'), '4-1' => array('八殿都市王诞'), '4-4' => array('文殊菩萨诞'), '4-8' => array('释迦牟尼佛诞'), '4-14' => array('纯阳祖师诞'), '4-15' => array('钟离祖师诞'), '4-17' => array('十殿转轮王诞'), '4-18' => array('紫徽大帝诞'), '4-20' => array('眼光圣母诞'), '5-1' => array('南极长生大帝诞'), '5-8' => array('南方五道诞'), '5-11' => array('天下都城隍诞'), '5-12' => array('炳灵公诞'), '5-13' => array('关圣降'), '5-16' => array('天地元气造化万物之辰'), '5-18' => array('张天师诞'), '5-22' => array('孝娥神诞'), '6-19' => array('观世音菩萨成道日'), '6-24' => array('关帝诞'), '7-7' => array('魁星诞'), '7-13' => array('长真谭真人诞', '大势至菩萨诞'), '7-15' => array('中元节'), '7-18' => array('西王母诞'), '7-19' => array('太岁诞'), '7-22' => array('增福财神诞'), '7-29' => array('杨公忌'), '7-30' => array('地藏菩萨诞'), '8-1' => array('许真君诞'), '8-3' => array('司命灶君诞'), '8-5' => array('雷声大帝诞'), '8-10' => array('北斗大帝诞'), '8-12' => array('西方五道诞'), '8-16' => array('天曹掠刷真君降'), '8-18' => array('天人兴福之辰'), '8-23' => array('汉恒候张显王诞'), '8-24' => array('灶君夫人诞'), '8-29' => array('至圣先师孔子诞'), '9-1' => array('北斗九星降世'), '9-3' => array('五瘟神诞'), '9-9' => array('酆都大帝诞'), '9-13' => array('孟婆尊神诞'), '9-17' => array('金龙四大王诞'), '9-19' => array('观世音菩萨出家'), '9-30' => array('药师琉璃光佛诞'), '10-1' => array('寒衣节'), '10-3' => array('三茅诞'), '10-5' => array('达摩祖师诞'), '10-8' => array('佛涅槃日'), '10-15' => array('下元节'), '11-4' => array('至圣先师孔子诞'), '11-6' => array('西岳大帝诞'), '11-11' => array('太乙救苦天尊诞'), '11-17' => array('阿弥陀佛诞'), '11-19' => array('太阳日宫诞'), '11-23' => array('张仙诞'), '11-26' => array('北方五道诞'), '12-8' => array('释迦如来成佛之辰'), '12-16' => array('南岳大帝诞'), '12-21' => array('天猷上帝诞'), '12-23' => array('小年'), '12-24' => array('子时灶君上天朝玉帝'), '12-29' => array('华严菩萨诞') ); public static $XIU = array( '申1' => '毕', '申2' => '翼', '申3' => '箕', '申4' => '奎', '申5' => '鬼', '申6' => '氐', '申0' => '虚', '子1' => '毕', '子2' => '翼', '子3' => '箕', '子4' => '奎', '子5' => '鬼', '子6' => '氐', '子0' => '虚', '辰1' => '毕', '辰2' => '翼', '辰3' => '箕', '辰4' => '奎', '辰5' => '鬼', '辰6' => '氐', '辰0' => '虚', '巳1' => '危', '巳2' => '觜', '巳3' => '轸', '巳4' => '斗', '巳5' => '娄', '巳6' => '柳', '巳0' => '房', '酉1' => '危', '酉2' => '觜', '酉3' => '轸', '酉4' => '斗', '酉5' => '娄', '酉6' => '柳', '酉0' => '房', '丑1' => '危', '丑2' => '觜', '丑3' => '轸', '丑4' => '斗', '丑5' => '娄', '丑6' => '柳', '丑0' => '房', '寅1' => '心', '寅2' => '室', '寅3' => '参', '寅4' => '角', '寅5' => '牛', '寅6' => '胃', '寅0' => '星', '午1' => '心', '午2' => '室', '午3' => '参', '午4' => '角', '午5' => '牛', '午6' => '胃', '午0' => '星', '戌1' => '心', '戌2' => '室', '戌3' => '参', '戌4' => '角', '戌5' => '牛', '戌6' => '胃', '戌0' => '星', '亥1' => '张', '亥2' => '尾', '亥3' => '壁', '亥4' => '井', '亥5' => '亢', '亥6' => '女', '亥0' => '昴', '卯1' => '张', '卯2' => '尾', '卯3' => '壁', '卯4' => '井', '卯5' => '亢', '卯6' => '女', '卯0' => '昴', '未1' => '张', '未2' => '尾', '未3' => '壁', '未4' => '井', '未5' => '亢', '未6' => '女', '未0' => '昴' ); public static $XIU_LUCK = array( '角' => '吉', '亢' => '凶', '氐' => '凶', '房' => '吉', '心' => '凶', '尾' => '吉', '箕' => '吉', '斗' => '吉', '牛' => '凶', '女' => '凶', '虚' => '凶', '危' => '凶', '室' => '吉', '壁' => '吉', '奎' => '凶', '娄' => '吉', '胃' => '吉', '昴' => '凶', '毕' => '吉', '觜' => '凶', '参' => '吉', '井' => '吉', '鬼' => '凶', '柳' => '凶', '星' => '凶', '张' => '吉', '翼' => '凶', '轸' => '吉' ); public static $XIU_SONG = array( '角' => '角星造作主荣昌,外进田财及女郎,嫁娶婚姻出贵子,文人及第见君王,惟有埋葬不可用,三年之后主瘟疫,起工修筑坟基地,堂前立见主人凶。', '亢' => '亢星造作长房当,十日之中主有殃,田地消磨官失职,接运定是虎狼伤,嫁娶婚姻用此日,儿孙新妇守空房,埋葬若还用此日,当时害祸主重伤。', '氐' => '氐星造作主灾凶,费尽田园仓库空,埋葬不可用此日,悬绳吊颈祸重重,若是婚姻离别散,夜招浪子入房中,行船必定遭沉没,更生聋哑子孙穷。', '房' => '房星造作田园进,钱财牛马遍山岗,更招外处田庄宅,荣华富贵福禄康,埋葬若然用此日,高官进职拜君王,嫁娶嫦娥至月殿,三年抱子至朝堂。', '心' => '心星造作大为凶,更遭刑讼狱囚中,忤逆官非宅产退,埋葬卒暴死相从,婚姻若是用此日,子死儿亡泪满胸,三年之内连遭祸,事事教君没始终。', '尾' => '尾星造作主天恩,富贵荣华福禄增,招财进宝兴家宅,和合婚姻贵子孙,埋葬若能依此日,男清女正子孙兴,开门放水招田宅,代代公侯远播名。', '箕' => '箕星造作主高强,岁岁年年大吉昌,埋葬修坟大吉利,田蚕牛马遍山岗,开门放水招田宅,箧满金银谷满仓,福荫高官加禄位,六亲丰禄乐安康。', '斗' => '斗星造作主招财,文武官员位鼎台,田宅家财千万进,坟堂修筑贵富来,开门放水招牛马,旺蚕男女主和谐,遇此吉宿来照护,时支福庆永无灾。', '牛' => '牛星造作主灾危,九横三灾不可推,家宅不安人口退,田蚕不利主人衰,嫁娶婚姻皆自损,金银财谷渐无之,若是开门并放水,牛猪羊马亦伤悲。', '女' => '女星造作损婆娘,兄弟相嫌似虎狼,埋葬生灾逢鬼怪,颠邪疾病主瘟惶,为事遭官财失散,泻利留连不可当,开门放水用此日,全家财散主离乡。', '虚' => '虚星造作主灾殃,男女孤眠不一双,内乱风声无礼节,儿孙媳妇伴人床,开门放水遭灾祸,虎咬蛇伤又卒亡,三三五五连年病,家破人亡不可当。', '危' => '危星不可造高楼,自遭刑吊见血光,三年孩子遭水厄,后生出外永不还,埋葬若还逢此日,周年百日取高堂,三年两载一悲伤,开门放水到官堂。', '室' => '室星修造进田牛,儿孙代代近王侯,家贵荣华天上至,寿如彭祖八千秋,开门放水招财帛,和合婚姻生贵儿,埋葬若能依此日,门庭兴旺福无休。', '壁' => '壁星造作主增财,丝蚕大熟福滔天,奴婢自来人口进,开门放水出英贤,埋葬招财官品进,家中诸事乐陶然,婚姻吉利主贵子,早播名誉著祖鞭。', '奎' => '奎星造作得祯祥,家内荣和大吉昌,若是埋葬阴卒死,当年定主两三伤,看看军令刑伤到,重重官事主瘟惶,开门放水遭灾祸,三年两次损儿郎。', '娄' => '娄星修造起门庭,财旺家和事事兴,外进钱财百日进,一家兄弟播高名,婚姻进益生贵子,玉帛金银箱满盈,放水开门皆吉利,男荣女贵寿康宁。', '胃' => '胃星造作事如何,家贵荣华喜气多,埋葬贵临官禄位,夫妇齐眉永保康,婚姻遇此家富贵,三灾九祸不逢他,从此门前多吉庆,儿孙代代拜金阶。', '昴' => '昴星造作进田牛,埋葬官灾不得休,重丧二日三人死,尽卖田园不记增,开门放水招灾祸,三岁孩儿白了头,婚姻不可逢此日,死别生离是可愁。', '毕' => '毕星造作主光前,买得田园有余钱,埋葬此日添官职,田蚕大熟永丰年,开门放水多吉庆,合家人口得安然,婚姻若得逢此日,生得孩儿福寿全。', '觜' => '觜星造作有徒刑,三年必定主伶丁,埋葬卒死多因此,取定寅年使杀人,三丧不止皆由此,一人药毒二人身,家门田地皆退败,仓库金银化作尘。', '参' => '参星造作旺人家,文星照耀大光华,只因造作田财旺,埋葬招疾哭黄沙,开门放水加官职,房房子孙见田加,婚姻许遁遭刑克,男女朝开幕落花。', '井' => '井星造作旺蚕田,金榜题名第一光,埋葬须防惊卒死,狂颠风疾入黄泉,开门放水招财帛,牛马猪羊旺莫言,贵人田塘来入宅,儿孙兴旺有余钱。', '鬼' => '鬼星起造卒人亡,堂前不见主人郎,埋葬此日官禄至,儿孙代代近君王,开门放水须伤死,嫁娶夫妻不久长,修土筑墙伤产女,手扶双女泪汪汪。', '柳' => '柳星造作主遭官,昼夜偷闭不暂安,埋葬瘟惶多疾病,田园退尽守冬寒,开门放水遭聋瞎,腰驼背曲似弓弯,更有棒刑宜谨慎,妇人随客走盘桓。', '星' => '星宿日好造新房,进职加官近帝王,不可埋葬并放水,凶星临位女人亡,生离死别无心恋,要自归休别嫁郎,孔子九曲殊难度,放水开门天命伤。', '张' => '张星日好造龙轩,年年并见进庄田,埋葬不久升官职,代代为官近帝前,开门放水招财帛,婚姻和合福绵绵,田蚕人满仓库满,百般顺意自安然。', '翼' => '翼星不利架高堂,三年二载见瘟惶,埋葬若还逢此日,子孙必定走他乡,婚姻此日不宜利,归家定是不相当,开门放水家须破,少女恋花贪外郎。', '轸' => '轸星临水造龙宫,代代为官受皇封,富贵荣华增寿禄,库满仓盈自昌隆,埋葬文昌来照助,宅舍安宁不见凶,更有为官沾帝宠,婚姻龙子入龙宫。' ); public static $SHOU = array( '东' => '青龙', '南' => '朱雀', '西' => '白虎', '北' => '玄武' ); public static $CHONG = array('', '午', '未', '申', '酉', '戌', '亥', '子', '丑', '寅', '卯', '辰', '巳'); public static $CHONG_GAN = array('', '戊', '己', '庚', '辛', '壬', '癸', '甲', '乙', '丙', '丁'); public static $CHONG_GAN_BAD = array( '庚' => '甲', '辛' => '乙', '壬' => '丙', '癸' => '丁' ); public static $CHONG_GAN_TIE = array('', '己', '戊', '辛', '庚', '癸', '壬', '乙', '甲', '丁', '丙'); public static $CHONG_GAN_TIE_GOOD = array( '甲' => '己', '丙' => '辛', '戊' => '癸', '庚' => '乙', '壬' => '丁' ); public static $SHA = array( '子' => '南', '丑' => '东', '寅' => '北', '卯' => '西', '辰' => '南', '巳' => '东', '午' => '北', '未' => '西', '申' => '南', '酉' => '东', '戌' => '北', '亥' => '西' ); public static $POSITION_DESC = array( '坎' => '正北', '艮' => '东北', '震' => '正东', '巽' => '东南', '离' => '正南', '坤' => '西南', '兑' => '正西', '乾' => '西北', '中' => '中宫' ); public static $GONG = array( '角' => '东', '井' => '南', '奎' => '西', '斗' => '北', '亢' => '东', '鬼' => '南', '娄' => '西', '牛' => '北', '氐' => '南', '柳' => '南', '胃' => '西', '女' => '北', '房' => '东', '星' => '南', '昴' => '西', '虚' => '北', '心' => '东', '张' => '南', '毕' => '西', '危' => '北', '尾' => '东', '翼' => '南', '觜' => '西', '室' => '北', '箕' => '东', '轸' => '南', '参' => '西', '壁' => '北' ); public static $ZHENG = array( '角' => '木', '井' => '木', '奎' => '木', '斗' => '木', '亢' => '金', '鬼' => '金', '娄' => '金', '牛' => '金', '氐' => '土', '柳' => '土', '胃' => '土', '女' => '土', '房' => '日', '星' => '日', '昴' => '日', '虚' => '日', '心' => '月', '张' => '月', '毕' => '月', '危' => '月', '尾' => '火', '翼' => '火', '觜' => '火', '室' => '火', '箕' => '水', '轸' => '水', '参' => '水', '壁' => '水' ); public static $ANIMAL = array( '角' => '蛟', '斗' => '獬', '奎' => '狼', '井' => '犴', '亢' => '龙', '牛' => '牛', '娄' => '狗', '鬼' => '羊', '女' => '蝠', '氐' => '貉', '胃' => '彘', '柳' => '獐', '房' => '兔', '虚' => '鼠', '昴' => '鸡', '星' => '马', '心' => '狐', '危' => '燕', '毕' => '乌', '张' => '鹿', '尾' => '虎', '室' => '猪', '觜' => '猴', '翼' => '蛇', '箕' => '豹', '壁' => '獝', '参' => '猿', '轸' => '蚓' ); public static $WU_XING_GAN = array( '甲' => '木', '乙' => '木', '丙' => '火', '丁' => '火', '戊' => '土', '己' => '土', '庚' => '金', '辛' => '金', '壬' => '水', '癸' => '水' ); public static $WU_XING_ZHI = array( '寅' => '木', '卯' => '木', '巳' => '火', '午' => '火', '辰' => '土', '丑' => '土', '戌' => '土', '未' => '土', '申' => '金', '酉' => '金', '亥' => '水', '子' => '水' ); public static $NAYIN = array( '甲子' => '海中金', '甲午' => '沙中金', '丙寅' => '炉中火', '丙申' => '山下火', '戊辰' => '大林木', '戊戌' => '平地木', '庚午' => '路旁土', '庚子' => '壁上土', '壬申' => '剑锋金', '壬寅' => '金箔金', '甲戌' => '山头火', '甲辰' => '覆灯火', '丙子' => '涧下水', '丙午' => '天河水', '戊寅' => '城头土', '戊申' => '大驿土', '庚辰' => '白蜡金', '庚戌' => '钗钏金', '壬午' => '杨柳木', '壬子' => '桑柘木', '甲申' => '泉中水', '甲寅' => '大溪水', '丙戌' => '屋上土', '丙辰' => '沙中土', '戊子' => '霹雳火', '戊午' => '天上火', '庚寅' => '松柏木', '庚申' => '石榴木', '壬辰' => '长流水', '壬戌' => '大海水', '乙丑' => '海中金', '乙未' => '沙中金', '丁卯' => '炉中火', '丁酉' => '山下火', '己巳' => '大林木', '己亥' => '平地木', '辛未' => '路旁土', '辛丑' => '壁上土', '癸酉' => '剑锋金', '癸卯' => '金箔金', '乙亥' => '山头火', '乙巳' => '覆灯火', '丁丑' => '涧下水', '丁未' => '天河水', '己卯' => '城头土', '己酉' => '大驿土', '辛巳' => '白蜡金', '辛亥' => '钗钏金', '癸未' => '杨柳木', '癸丑' => '桑柘木', '乙酉' => '泉中水', '乙卯' => '大溪水', '丁亥' => '屋上土', '丁巳' => '沙中土', '己丑' => '霹雳火', '己未' => '天上火', '辛卯' => '松柏木', '辛酉' => '石榴木', '癸巳' => '长流水', '癸亥' => '大海水' ); public static $SHI_SHEN_GAN = array( '甲甲' => '比肩', '甲乙' => '劫财', '甲丙' => '食神', '甲丁' => '伤官', '甲戊' => '偏财', '甲己' => '正财', '甲庚' => '七杀', '甲辛' => '正官', '甲壬' => '偏印', '甲癸' => '正印', '乙乙' => '比肩', '乙甲' => '劫财', '乙丁' => '食神', '乙丙' => '伤官', '乙己' => '偏财', '乙戊' => '正财', '乙辛' => '七杀', '乙庚' => '正官', '乙癸' => '偏印', '乙壬' => '正印', '丙丙' => '比肩', '丙丁' => '劫财', '丙戊' => '食神', '丙己' => '伤官', '丙庚' => '偏财', '丙辛' => '正财', '丙壬' => '七杀', '丙癸' => '正官', '丙甲' => '偏印', '丙乙' => '正印', '丁丁' => '比肩', '丁丙' => '劫财', '丁己' => '食神', '丁戊' => '伤官', '丁辛' => '偏财', '丁庚' => '正财', '丁癸' => '七杀', '丁壬' => '正官', '丁乙' => '偏印', '丁甲' => '正印', '戊戊' => '比肩', '戊己' => '劫财', '戊庚' => '食神', '戊辛' => '伤官', '戊壬' => '偏财', '戊癸' => '正财', '戊甲' => '七杀', '戊乙' => '正官', '戊丙' => '偏印', '戊丁' => '正印', '己己' => '比肩', '己戊' => '劫财', '己辛' => '食神', '己庚' => '伤官', '己癸' => '偏财', '己壬' => '正财', '己乙' => '七杀', '己甲' => '正官', '己丁' => '偏印', '己丙' => '正印', '庚庚' => '比肩', '庚辛' => '劫财', '庚壬' => '食神', '庚癸' => '伤官', '庚甲' => '偏财', '庚乙' => '正财', '庚丙' => '七杀', '庚丁' => '正官', '庚戊' => '偏印', '庚己' => '正印', '辛辛' => '比肩', '辛庚' => '劫财', '辛癸' => '食神', '辛壬' => '伤官', '辛乙' => '偏财', '辛甲' => '正财', '辛丁' => '七杀', '辛丙' => '正官', '辛己' => '偏印', '辛戊' => '正印', '壬壬' => '比肩', '壬癸' => '劫财', '壬甲' => '食神', '壬乙' => '伤官', '壬丙' => '偏财', '壬丁' => '正财', '壬戊' => '七杀', '壬己' => '正官', '壬庚' => '偏印', '壬辛' => '正印', '癸癸' => '比肩', '癸壬' => '劫财', '癸乙' => '食神', '癸甲' => '伤官', '癸丁' => '偏财', '癸丙' => '正财', '癸己' => '七杀', '癸戊' => '正官', '癸辛' => '偏印', '癸庚' => '正印' ); public static $SHI_SHEN_ZHI = array( '甲子癸' => '正印', '甲丑癸' => '正印', '甲丑己' => '正财', '甲丑辛' => '正官', '甲寅丙' => '食神', '甲寅甲' => '比肩', '甲寅戊' => '偏财', '甲卯乙' => '劫财', '甲辰乙' => '劫财', '甲辰戊' => '偏财', '甲辰癸' => '正印', '甲巳戊' => '偏财', '甲巳丙' => '食神', '甲巳庚' => '七杀', '甲午丁' => '伤官', '甲午己' => '正财', '甲未乙' => '劫财', '甲未己' => '正财', '甲未丁' => '伤官', '甲申戊' => '偏财', '甲申庚' => '七杀', '甲申壬' => '偏印', '甲酉辛' => '正官', '甲戌辛' => '正官', '甲戌戊' => '偏财', '甲戌丁' => '伤官', '甲亥壬' => '偏印', '甲亥甲' => '比肩', '乙子癸' => '偏印', '乙丑癸' => '偏印', '乙丑己' => '偏财', '乙丑辛' => '七杀', '乙寅丙' => '伤官', '乙寅甲' => '劫财', '乙寅戊' => '正财', '乙卯乙' => '比肩', '乙辰乙' => '比肩', '乙辰戊' => '正财', '乙辰癸' => '偏印', '乙巳戊' => '正财', '乙巳丙' => '伤官', '乙巳庚' => '正官', '乙午丁' => '食神', '乙午己' => '偏财', '乙未乙' => '比肩', '乙未己' => '偏财', '乙未丁' => '食神', '乙申戊' => '正财', '乙申庚' => '正官', '乙申壬' => '正印', '乙酉辛' => '七杀', '乙戌辛' => '七杀', '乙戌戊' => '正财', '乙戌丁' => '食神', '乙亥壬' => '正印', '乙亥甲' => '劫财', '丙子癸' => '正官', '丙丑癸' => '正官', '丙丑己' => '伤官', '丙丑辛' => '正财', '丙寅丙' => '比肩', '丙寅甲' => '偏印', '丙寅戊' => '食神', '丙卯乙' => '正印', '丙辰乙' => '正印', '丙辰戊' => '食神', '丙辰癸' => '正官', '丙巳戊' => '食神', '丙巳丙' => '比肩', '丙巳庚' => '偏财', '丙午丁' => '劫财', '丙午己' => '伤官', '丙未乙' => '正印', '丙未己' => '伤官', '丙未丁' => '劫财', '丙申戊' => '食神', '丙申庚' => '偏财', '丙申壬' => '七杀', '丙酉辛' => '正财', '丙戌辛' => '正财', '丙戌戊' => '食神', '丙戌丁' => '劫财', '丙亥壬' => '七杀', '丙亥甲' => '偏印', '丁子癸' => '七杀', '丁丑癸' => '七杀', '丁丑己' => '食神', '丁丑辛' => '偏财', '丁寅丙' => '劫财', '丁寅甲' => '正印', '丁寅戊' => '伤官', '丁卯乙' => '偏印', '丁辰乙' => '偏印', '丁辰戊' => '伤官', '丁辰癸' => '七杀', '丁巳戊' => '伤官', '丁巳丙' => '劫财', '丁巳庚' => '正财', '丁午丁' => '比肩', '丁午己' => '食神', '丁未乙' => '偏印', '丁未己' => '食神', '丁未丁' => '比肩', '丁申戊' => '伤官', '丁申庚' => '正财', '丁申壬' => '正官', '丁酉辛' => '偏财', '丁戌辛' => '偏财', '丁戌戊' => '伤官', '丁戌丁' => '比肩', '丁亥壬' => '正官', '丁亥甲' => '正印', '戊子癸' => '正财', '戊丑癸' => '正财', '戊丑己' => '劫财', '戊丑辛' => '伤官', '戊寅丙' => '偏印', '戊寅甲' => '七杀', '戊寅戊' => '比肩', '戊卯乙' => '正官', '戊辰乙' => '正官', '戊辰戊' => '比肩', '戊辰癸' => '正财', '戊巳戊' => '比肩', '戊巳丙' => '偏印', '戊巳庚' => '食神', '戊午丁' => '正印', '戊午己' => '劫财', '戊未乙' => '正官', '戊未己' => '劫财', '戊未丁' => '正印', '戊申戊' => '比肩', '戊申庚' => '食神', '戊申壬' => '偏财', '戊酉辛' => '伤官', '戊戌辛' => '伤官', '戊戌戊' => '比肩', '戊戌丁' => '正印', '戊亥壬' => '偏财', '戊亥甲' => '七杀', '己子癸' => '偏财', '己丑癸' => '偏财', '己丑己' => '比肩', '己丑辛' => '食神', '己寅丙' => '正印', '己寅甲' => '正官', '己寅戊' => '劫财', '己卯乙' => '七杀', '己辰乙' => '七杀', '己辰戊' => '劫财', '己辰癸' => '偏财', '己巳戊' => '劫财', '己巳丙' => '正印', '己巳庚' => '伤官', '己午丁' => '偏印', '己午己' => '比肩', '己未乙' => '七杀', '己未己' => '比肩', '己未丁' => '偏印', '己申戊' => '劫财', '己申庚' => '伤官', '己申壬' => '正财', '己酉辛' => '食神', '己戌辛' => '食神', '己戌戊' => '劫财', '己戌丁' => '偏印', '己亥壬' => '正财', '己亥甲' => '正官', '庚子癸' => '伤官', '庚丑癸' => '伤官', '庚丑己' => '正印', '庚丑辛' => '劫财', '庚寅丙' => '七杀', '庚寅甲' => '偏财', '庚寅戊' => '偏印', '庚卯乙' => '正财', '庚辰乙' => '正财', '庚辰戊' => '偏印', '庚辰癸' => '伤官', '庚巳戊' => '偏印', '庚巳丙' => '七杀', '庚巳庚' => '比肩', '庚午丁' => '正官', '庚午己' => '正印', '庚未乙' => '正财', '庚未己' => '正印', '庚未丁' => '正官', '庚申戊' => '偏印', '庚申庚' => '比肩', '庚申壬' => '食神', '庚酉辛' => '劫财', '庚戌辛' => '劫财', '庚戌戊' => '偏印', '庚戌丁' => '正官', '庚亥壬' => '食神', '庚亥甲' => '偏财', '辛子癸' => '食神', '辛丑癸' => '食神', '辛丑己' => '偏印', '辛丑辛' => '比肩', '辛寅丙' => '正官', '辛寅甲' => '正财', '辛寅戊' => '正印', '辛卯乙' => '偏财', '辛辰乙' => '偏财', '辛辰戊' => '正印', '辛辰癸' => '食神', '辛巳戊' => '正印', '辛巳丙' => '正官', '辛巳庚' => '劫财', '辛午丁' => '七杀', '辛午己' => '偏印', '辛未乙' => '偏财', '辛未己' => '偏印', '辛未丁' => '七杀', '辛申戊' => '正印', '辛申庚' => '劫财', '辛申壬' => '伤官', '辛酉辛' => '比肩', '辛戌辛' => '比肩', '辛戌戊' => '正印', '辛戌丁' => '七杀', '辛亥壬' => '伤官', '辛亥甲' => '正财', '壬子癸' => '劫财', '壬丑癸' => '劫财', '壬丑己' => '正官', '壬丑辛' => '正印', '壬寅丙' => '偏财', '壬寅甲' => '食神', '壬寅戊' => '七杀', '壬卯乙' => '伤官', '壬辰乙' => '伤官', '壬辰戊' => '七杀', '壬辰癸' => '劫财', '壬巳戊' => '七杀', '壬巳丙' => '偏财', '壬巳庚' => '偏印', '壬午丁' => '正财', '壬午己' => '正官', '壬未乙' => '伤官', '壬未己' => '正官', '壬未丁' => '正财', '壬申戊' => '七杀', '壬申庚' => '偏印', '壬申壬' => '比肩', '壬酉辛' => '正印', '壬戌辛' => '正印', '壬戌戊' => '七杀', '壬戌丁' => '正财', '壬亥壬' => '比肩', '壬亥甲' => '食神', '癸子癸' => '比肩', '癸丑癸' => '比肩', '癸丑己' => '七杀', '癸丑辛' => '偏印', '癸寅丙' => '正财', '癸寅甲' => '伤官', '癸寅戊' => '正官', '癸卯乙' => '食神', '癸辰乙' => '食神', '癸辰戊' => '正官', '癸辰癸' => '比肩', '癸巳戊' => '正官', '癸巳丙' => '正财', '癸巳庚' => '正印', '癸午丁' => '偏财', '癸午己' => '七杀', '癸未乙' => '食神', '癸未己' => '七杀', '癸未丁' => '偏财', '癸申戊' => '正官', '癸申庚' => '正印', '癸申壬' => '劫财', '癸酉辛' => '偏印', '癸戌辛' => '偏印', '癸戌戊' => '正官', '癸戌丁' => '偏财', '癸亥壬' => '劫财', '癸亥甲' => '伤官' ); public static $ZHI_HIDE_GAN = array( '子' => array('癸'), '丑' => array('己', '癸', '辛'), '寅' => array('甲', '丙', '戊'), '卯' => array('乙'), '辰' => array('戊', '乙', '癸'), '巳' => array('丙', '庚', '戊'), '午' => array('丁', '己'), '未' => array('己', '丁', '乙'), '申' => array('庚', '壬', '戊'), '酉' => array('辛'), '戌' => array('戊', '辛', '丁'), '亥' => array('壬', '甲') ); /** * 计算指定日期距离基准日期的天数 * @param int $year 农历年 * @param int $month 农历月 * @param int $day 农历日 * @return int 距离天数 */ public static function computeAddDays($year, $month, $day) { if (LunarUtil::$BASE_YEAR == $year && LunarUtil::$BASE_MONTH == $month) { return $day - LunarUtil::$BASE_DAY; } $y = LunarUtil::$BASE_YEAR; $m = LunarUtil::$BASE_MONTH; $diff = LunarUtil::getDaysOfMonth($y, $m) - LunarUtil::$BASE_DAY; while ($y != $year || $m != $month) { $m = LunarUtil::nextMonth($y, $m); if ($m == 1) { $y++; } if ($y == $year && $m == $month) { $diff += $day; } else { $diff += LunarUtil::getDaysOfMonth($y, $m); } } return $diff; } /** * 获取指定年份的闰月,1代表闰1月,0代表无闰月 * @param int $year 农历年 * @return int 闰月数字,1代表闰1月,0代表无闰月 */ public static function getLeapMonth($year) { $index = $year - LunarUtil::$BASE_YEAR + LunarUtil::$BASE_INDEX; $v = LunarUtil::$LUNAR_MONTH[2 * $index + 1]; $v = ($v >> 4) & 0x0F; return $v; } /** * 获取指定年月的下一个月是第几月,1到12,闰月为负数 * @param int $y 农历年 * @param int $m 农历月,闰月为负数 * @return int 1到12,闰月为负数 */ public static function nextMonth($y, $m) { $n = abs($m) + 1; if ($m > 0) { if ($m == LunarUtil::getLeapMonth($y)) { $n = -$m; } } return 13 != $n ? $n : 1; } /** * 获取某年某月有多少天 * @param int $year 农历年 * @param int $month 农历月,闰月为负数 * @return int 天数 */ public static function getDaysOfMonth($year, $month) { $index = $year - LunarUtil::$BASE_YEAR + LunarUtil::$BASE_INDEX; $d = 30; if (1 <= $month && $month <= 8) { $v = LunarUtil::$LUNAR_MONTH[2 * $index]; $l = $month - 1; if ((($v >> $l) & 0x01) === 1) { $d = 29; } } else if (9 <= $month && $month <= 12) { $v = LunarUtil::$LUNAR_MONTH[2 * $index + 1]; $l = $month - 9; if ((($v >> $l) & 0x01) === 1) { $d = 29; } } else { $v = LunarUtil::$LUNAR_MONTH[2 * $index + 1]; $v = ($v >> 4) & 0x0F; if ($v !== abs($month)) { $d = 0; } else { $d = 29; foreach (LunarUtil::$LEAP_MONTH_YEAR as $i) { if ($i === $index) { $d = 30; break; } } } } return $d; } /** * 获取HH:mm时刻的地支序号(0到11),非法的时刻返回0 * @param string $hm HH:mm时刻 * @return int 地支序号,0到11 */ public static function getTimeZhiIndex($hm) { if (null == $hm) { return 0; } if (strlen($hm) > 5) { $hm = substr($hm, 0, 5); } $x = 1; for ($i = 1; $i < 22; $i += 2) { if (strcmp($hm, ($i < 10 ? '0' : '') . $i . ':00') >= 0 && strcmp($hm, ($i + 1 < 10 ? '0' : '') . ($i + 1) . ':59') <= 0) { return $x; } $x++; } return 0; } /** * 将HH:mm时刻转换为时辰(地支),非法的时刻返回子 * @param string $hm HH:mm时刻 * @return string 时辰(地支),如子 */ public static function convertTime($hm) { return LunarUtil::$ZHI[LunarUtil::getTimeZhiIndex($hm) + 1]; } /** * 数字转十六进制 * @param int $n 数字 * @return string 十六进制 */ private static function hex($n) { $s = dechex($n); if (strlen($s) < 2) { $s = '0' . $s; } return strtoupper($s); } /** * 获取干支对应的甲子序号 * @param string $ganZhi 干支 * @return int 甲子序号 */ public static function getJiaZiIndex($ganZhi) { for ($i = 0, $j = count(LunarUtil::$JIA_ZI); $i < $j; $i++) { if (strcmp(LunarUtil::$JIA_ZI[$i], $ganZhi) === 0) { return $i; } } return -1; } /** * 获取日宜 * @param string $monthGanZhi 月干支 * @param string $dayGanZhi 日干支 * @return array 宜 */ public static function getDayYi($monthGanZhi, $dayGanZhi) { $l = array(); $day = LunarUtil::hex(LunarUtil::getJiaZiIndex($dayGanZhi)); $month = LunarUtil::hex(LunarUtil::getJiaZiIndex($monthGanZhi)); $right = LunarUtil::$DAY_YI_JI; $index = strpos($right, $day . '='); while ($index) { $right = substr($right, $index + 3); $left = $right; if (strpos($left, '=')) { $left = substr($left, 0, strpos($left, '=') - 2); } $matched = false; $months = substr($left, 0, strpos($left, ':')); for ($i = 0, $j = strlen($months); $i < $j; $i += 2) { $m = substr($months, $i, 2); if ($m == $month) { $matched = true; break; } } if ($matched) { $ys = substr($left, strpos($left, ':') + 1, strlen($left)); $ys = substr($ys, 0, strpos($ys, ',')); for ($i = 0, $j = strlen($ys); $i < $j; $i += 2) { $m = substr($ys, $i, 2); $l[] = LunarUtil::$YI_JI[hexdec($m)]; } break; } $index = strpos($right, $day . '='); } if (count($l) < 1) { $l[] = '无'; } return $l; } /** * 获取日忌 * @param string $monthGanZhi 月干支 * @param string $dayGanZhi 日干支 * @return array 忌 */ public static function getDayJi($monthGanZhi, $dayGanZhi) { $l = array(); $day = LunarUtil::hex(LunarUtil::getJiaZiIndex($dayGanZhi)); $month = LunarUtil::hex(LunarUtil::getJiaZiIndex($monthGanZhi)); $right = LunarUtil::$DAY_YI_JI; $index = strpos($right, $day . '='); while ($index) { $right = substr($right, $index + 3); $left = $right; if (strpos($left, '=')) { $left = substr($left, 0, strpos($left, '=') - 2); } $matched = false; $months = substr($left, 0, strpos($left, ':')); for ($i = 0, $j = strlen($months); $i < $j; $i += 2) { $m = substr($months, $i, 2); if ($m == $month) { $matched = true; break; } } if ($matched) { $ys = substr($left, strpos($left, ',') + 1, strlen($left)); for ($i = 0, $j = strlen($ys); $i < $j; $i += 2) { $m = substr($ys, $i, 2); $l[] = LunarUtil::$YI_JI[hexdec($m)]; } break; } $index = strpos($right, $day . '='); } if (count($l) < 1) { $l[] = '无'; } return $l; } /** * 获取日吉神 * @param int $lunarMonth 月 * @param string $dayGanZhi 日干支 * @return array 吉神 */ public static function getDayJiShen($lunarMonth, $dayGanZhi) { $l = array(); $day = LunarUtil::hex(LunarUtil::getJiaZiIndex($dayGanZhi)); $month = strtoupper(dechex(abs($lunarMonth))); $index = strpos(LunarUtil::$DAY_SHEN_SHA, $month . $day . '='); if ($index > -1) { $left = substr(LunarUtil::$DAY_SHEN_SHA, $index + 4); if (strpos($left, '=')) { $left = substr($left, 0, strpos($left, '=') - 3); } $js = substr($left, 0, strpos($left, ',')); for ($i = 0, $j = strlen($js); $i < $j; $i += 2) { $m = substr($js, $i, 2); $l[] = LunarUtil::$SHEN_SHA[hexdec($m)]; } } if (count($l) < 1) { $l[] = '无'; } return $l; } /** * 获取日凶煞 * @param int $lunarMonth 月 * @param string $dayGanZhi 日干支 * @return array 凶煞 */ public static function getDayXiongSha($lunarMonth, $dayGanZhi) { $l = array(); $day = LunarUtil::hex(LunarUtil::getJiaZiIndex($dayGanZhi)); $month = strtoupper(dechex(abs($lunarMonth))); $index = strpos(LunarUtil::$DAY_SHEN_SHA, $month . $day . '='); if ($index > -1) { $left = substr(LunarUtil::$DAY_SHEN_SHA, $index + 4); if (strpos($left, '=')) { $left = substr($left, 0, strpos($left, '=') - 3); } $xs = substr($left, strpos($left, ',') + 1, strlen($left)); for ($i = 0, $j = strlen($xs); $i < $j; $i += 2) { $m = substr($xs, $i, 2); $l[] = LunarUtil::$SHEN_SHA[hexdec($m)]; } } if (count($l) < 1) { $l[] = '无'; } return $l; } /** * 获取时宜 * @param string $dayGanZhi 日干支 * @param string $timeGanZhi 时辰干支 * @return array 宜 */ public static function getTimeYi($dayGanZhi, $timeGanZhi) { $l = array(); $day = LunarUtil::hex(LunarUtil::getJiaZiIndex($dayGanZhi)); $time = LunarUtil::hex(LunarUtil::getJiaZiIndex($timeGanZhi)); $index = strpos(LunarUtil::$TIME_YI_JI, $day . $time . '='); if ($index > -1) { $left = substr(LunarUtil::$TIME_YI_JI, $index + 5); if (strpos($left, '=')) { $left = substr($left, 0, strpos($left, '=') - 4); } $ys = substr($left, 0, strpos($left, ',')); for ($i = 0, $j = strlen($ys); $i < $j; $i += 2) { $m = substr($ys, $i, 2); $l[] = LunarUtil::$YI_JI[hexdec($m)]; } } if (count($l) < 1) { $l[] = '无'; } return $l; } /** * 获取时忌 * @param string $dayGanZhi 日干支 * @param string $timeGanZhi 时辰干支 * @return array 忌 */ public static function getTimeJi($dayGanZhi, $timeGanZhi) { $l = array(); $day = LunarUtil::hex(LunarUtil::getJiaZiIndex($dayGanZhi)); $time = LunarUtil::hex(LunarUtil::getJiaZiIndex($timeGanZhi)); $index = strpos(LunarUtil::$TIME_YI_JI, $day . $time . '='); if ($index > -1) { $left = substr(LunarUtil::$TIME_YI_JI, $index + 5); if (strpos($left, '=')) { $left = substr($left, 0, strpos($left, '=') - 4); } $js = substr($left, strpos($left, ',') + 1, strlen($left)); for ($i = 0, $j = strlen($js); $i < $j; $i += 2) { $m = substr($js, $i, 2); $l[] = LunarUtil::$YI_JI[hexdec($m)]; } } if (count($l) < 1) { $l[] = '无'; } return $l; } /** * 获取干支所在旬下标,0-5 * @param string $ganZhi 干支 * @return int 旬下标,0-5 */ private static function getXunIndex($ganZhi) { $half = strlen($ganZhi) / 2; $gan = substr($ganZhi, 0 ,$half); $zhi = substr($ganZhi, $half); $ganIndex = 0; $zhiIndex = 0; for ($i = 0, $j = count(LunarUtil::$GAN); $i < $j; $i++) { if (strcmp(LunarUtil::$GAN[$i], $gan) === 0) { $ganIndex = $i; break; } } for ($i = 0, $j = count(LunarUtil::$ZHI); $i < $j; $i++) { if (strcmp(LunarUtil::$ZHI[$i], $zhi) === 0) { $zhiIndex = $i; break; } } $diff = $ganIndex - $zhiIndex; if ($diff < 0) { $diff += 12; } return $diff / 2; } /** * 获取干支所在旬 * @param string ganZhi 干支 * @return string 旬 */ public static function getXun($ganZhi) { return LunarUtil::$XUN[LunarUtil::getXunIndex($ganZhi)]; } /** * 获取干支所在旬对应的旬空(空亡) * @param string ganZhi 干支 * @return string 旬空(空亡) */ public static function getXunKong($ganZhi) { return LunarUtil::$XUN_KONG[LunarUtil::getXunIndex($ganZhi)]; } }src/util/SolarUtil.php000064400000013364146412202250010744 0ustar00 '元旦节', '2-14' => '情人节', '3-8' => '妇女节', '3-12' => '植树节', '3-15' => '消费者权益日', '4-1' => '愚人节', '5-1' => '劳动节', '5-4' => '青年节', '6-1' => '儿童节', '7-1' => '建党节', '8-1' => '建军节', '9-10' => '教师节', '10-1' => '国庆节', '12-24' => '平安夜', '12-25' => '圣诞节' ); /** * 几月第几个星期几对应的节日 * @var array */ public static $WEEK_FESTIVAL = array( '5-2-0' => '母亲节', '6-3-0' => '父亲节', '11-4-4' => '感恩节' ); /** * 日期对应的非正式节日 * @var array */ public static $OTHER_FESTIVAL = array( '1-8' => array('周恩来逝世纪念日'), '1-10' => array('中国人民警察节', '中国公安110宣传日'), '1-21' => array('列宁逝世纪念日'), '1-26' => array('国际海关日'), '2-2' => array('世界湿地日'), '2-4' => array('世界抗癌日'), '2-7' => array('京汉铁路罢工纪念'), '2-10' => array('国际气象节'), '2-19' => array('邓小平逝世纪念日'), '2-21' => array('国际母语日'), '2-24' => array('第三世界青年日'), '3-1' => array('国际海豹日'), '3-3' => array('全国爱耳日'), '3-5' => array('周恩来诞辰纪念日', '中国青年志愿者服务日'), '3-6' => array('世界青光眼日'), '3-12' => array('孙中山逝世纪念日'), '3-14' => array('马克思逝世纪念日'), '3-17' => array('国际航海日'), '3-18' => array('全国科技人才活动日'), '3-21' => array('世界森林日', '世界睡眠日'), '3-22' => array('世界水日'), '3-23' => array('世界气象日'), '3-24' => array('世界防治结核病日'), '4-2' => array('国际儿童图书日'), '4-7' => array('世界卫生日'), '4-22' => array('列宁诞辰纪念日'), '4-23' => array('世界图书和版权日'), '4-26' => array('世界知识产权日'), '5-3' => array('世界新闻自由日'), '5-5' => array('马克思诞辰纪念日'), '5-8' => array('世界红十字日'), '5-11' => array('世界肥胖日'), '5-23' => array('世界读书日'), '5-27' => array('上海解放日'), '5-31' => array('世界无烟日'), '6-5' => array('世界环境日'), '6-6' => array('全国爱眼日'), '6-8' => array('世界海洋日'), '6-11' => array('中国人口日'), '6-14' => array('世界献血日'), '7-1' => array('香港回归纪念日'), '7-7' => array('中国人民抗日战争纪念日'), '7-11' => array('世界人口日'), '8-5' => array('恩格斯逝世纪念日'), '8-6' => array('国际电影节'), '8-12' => array('国际青年日'), '8-22' => array('邓小平诞辰纪念日'), '9-3' => array('中国抗日战争胜利纪念日'), '9-8' => array('世界扫盲日'), '9-9' => array('毛泽东逝世纪念日'), '9-14' => array('世界清洁地球日'), '9-18' => array('九一八事变纪念日'), '9-20' => array('全国爱牙日'), '9-21' => array('国际和平日'), '9-27' => array('世界旅游日'), '10-4' => array('世界动物日'), '10-10' => array('辛亥革命纪念日'), '10-13' => array('中国少年先锋队诞辰日'), '10-25' => array('抗美援朝纪念日'), '11-12' => array('孙中山诞辰纪念日'), '11-17' => array('国际大学生节'), '11-28' => array('恩格斯诞辰纪念日'), '12-1' => array('世界艾滋病日'), '12-12' => array('西安事变纪念日'), '12-13' => array('南京大屠杀纪念日'), '12-26' => array('毛泽东诞辰纪念日') ); /** * 是否闰年 * @param int $year 年 * @return bool 是否闰年 */ public static function isLeapYear($year) { $leap = false; if ($year % 4 == 0) { $leap = true; } if ($year % 100 == 0) { $leap = false; } if ($year % 400 == 0) { $leap = true; } return $leap; } /** * 获取某年某月有多少天 * @param int $year 年 * @param int $month 月 * @return int 天数 */ public static function getDaysOfMonth($year, $month) { $d = SolarUtil::$DAYS_OF_MONTH[$month - 1]; //公历闰年2月多一天 if ($month == 2 && SolarUtil::isLeapYear($year)) { $d++; } return $d; } /** * 获取某年某月有多少周 * @param int $year 年 * @param int $month 月 * @param int $start 星期几作为一周的开始,1234560分别代表星期一至星期天 * @return int 周数 */ public static function getWeeksOfMonth($year, $month, $start) { $days = SolarUtil::getDaysOfMonth($year, $month); $week = date('w', strtotime($year . '-' . $month . '-1')); return ceil(($days + $week - $start) / count(SolarUtil::$WEEK)); } }