php 计算阴历、阳历、属性、星座方法
<?php // 公历,农历转换,属相,星座的计算 /** * 1912年到2135年的数据 * *************************************************************************** * 前二数字 = 闰月月份, 如果为 13 则没有闰月 * * 第叁至第六数字 = 12 个月之大小月之2进位码->10进位 * * 例如: * * 101010101010 = 2730 * * 1 : 代表大月(30天) 0 : 代表小月(29天) ==> 1月大2月小3月大.. * * 第七位数字为闰月天数 * * 0 : 没有闰月之天数 * * 1 : 闰月为小月(29天) * * 2 : 闰月为大月(30天) * * 最後2位数字代表阳历之1月1日与阴历之1月1日相差天数 * *************************************************************************** */ class Lunar { var $LMDay = array(); var $InterMonth = 0; var $InterMonthDays = 0; var $SLRangeDay = 0; var $SMDay = array(1 => 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31); var $LongLife = array( 1 => '021327123', '131175041', '091614130', '133402048', '133749037', '06938227', '131389044', '13694034', '032142224', '132350043', '113223131', '132709050', '133402039', '083498128', '132901046', '131387035', '04730126', '132653044', '132349033', '023371122', '132709041', '092893130', '131706047', '132773037', '061397127', '131210045', '132651034', '031111224', '131323042', '112708232', '133733049', '131706038', '082778128', '132485047', '131206035', '042734125', '132639044', '131318033', '023366221', '133413040', '101449230', '131386048', '132413037', '061181227', '131182045', '132637035', '043405123', '133365042', '113411131', '132900050', '132906038', '082394228', '132395047', '131179036', '042711125', '132635044', '132725033', '013749122', '131748039', '112779129', '13694048', '132359038', '061199127', '131175045', '131611034', '031866124', '133493041', '111717130', '131389049', '13686039', '072366128', '132350047', '133222036', '053349225', '133402043', '133493032', '01853222', '131386040', '102670129', '132653048', '132349038', '062731127', '132709045', '132890034', '042986123', '132773042', '131370030', '022234220', '132651039', '071335129', '131323046', '131683035', '051813225', '131706043', '132773032', '031461122', '131206040', '082654130', '132638048', '133366037', '063750126', '133410045', '133498033', '041642223', '131389041', '131198031', '032717120', '132637039', '073371128', '132853047', '133410035', '053412224', '132906043', '131373032', '031371122', '131179040', '072647130', '132635049', '132725038', '062917127', '131746045', '132778035', '041462124', '132359043', '131175032', '031687122', '131611039', '071706129', '133493047', '131450036', '052796125', '132734044', '132350034', '033374123', '133222041', '083397230', '133402049', '133413038', '061429227', '131386045', '132670035', '041373125', '131325042', '132709032', '033221221', '132890040', '072890228', '132773047', '131370036', '052618226', '132651044', '131323033', '042583223', '131683041', '091707130', '131706048', '132741038', '061269128', '131206045', '132647035', '041326125', '133350043', '133731031', '031874121', '133498039', '071450229', '131389046', '131198036', '052589226', '132605045', '133349033', '043493122', '132898041', '093434130', '132778048', '131373037', '06667228', '131115046', '132603035', '052859124', '132709043', '132898032', '022738220', '132774039', '071366229', '132359048', '131111036', '051623126', '131323044', '131685033', '031941122', '131450040', '102742130', '132669049', '131198037', '062670127', '132646046', '133402035', '053754123', '133413042', '131450031', '032922121', '132669039', '071213129', '131195047', '132701037', '053413125', '132858044', '132901033', '041493123', '131242040', '132397030', '021367120', '131179038', '062711127', '131611045', '131705034', '042986124', '131717042', '13698032', '022742122', '132359041', '061326130', '133350048', '133659037', '051746127', '133497044', '131461033', '03877124', '13686042', '132606031', '023629120', '133221039', '063413128', '132898046', '132921035', '041498125', '131373043', '13605032', '032651122', '131579040', '072699230', '132709048', '132890037', '052858226', '132773045', '131371033', '04695124', '13599042', '091327131', '131323049', '131685038', '061749128', '131450046', '132741035', '041389125', '131198043', '132647033', '031109222', '133370040', '073722229', '133413048', '131450036', '052778126', '132397045', '131198034', '042731123', '132637042', '083371131', '132857050', '132901038', '071397128', '13730046', '132397036', '051239125', '131179043', '132635033', '032635222', '131705039', '082777129', '131717047', '13694037', '052358226', '132359045', '131175034', '041686124', '133658041', '103750130', '133497049', '131453038', '06685228', '13686046', '132350036', '053245125', '133221043', '133402032', '033402221', '132921040', '071402129', '131371047', '13605037', '052395127', '132347045', '132709034', '043477123', '132890042', '132837030', '021749120', '131371038', '06631129', '13599046', '131323035', '052730125', '133733043', '131706031', '032986121', '132741040', '081213130', '131198048', '132647038', '051357128', '133366046', '133477034', '041621224', '131386042', '132477032', '021373121', '131198039', '132637048', '133365036', '053365225', '132900044', '131386034', '022778122', //6 '132395041', '071175231', '131175050', '132635038', '052891127', '131701046', //12 '131748035', '042741223', '130694043', '132391032', '021327122', '131175040', //18 '061623129', '133402047', '133402036', '051769125', '131453044', '130694034', //24 '032158223', '132350041', '073213230', '133221049', '133402038', '063466226', //30 '132901045', '131130035', '042651224', '130605043', '132349032', '023371121', //36 '132709040', '072901128', '131738047', '132901036', '051333226', '131210044', //42 '132651033', '031111223', '131323042', '082714130', '133733048', '131706038', //48 '062794127', '132741045', '131206035', '042734124', '132647043', '131318032', //54 '033878120', '133477039', '071461129', '131386047', '132413036', '051245126', //60 '131197045', '132637033', '043405122', '133365041', '083413130', '132900048', //66 '132922037', '062394227', '132395046', '131179035', '042711124', '132635043', //72 '102855132', '131701050', '131748039', '062804128', '132742047', '132359036', //78 '051199126', '131175045', '131611034', '031866122', '133749040', '081717130', //84 '131452049', '132742037', '052413127', '132350046', '133222035', '043477123', //90 '133402042', '133493031', '021877121', '131386039', '072747128', '130605048', //96 '132349037', '053243125', '132709044', '132890033', '042986122', '132773040', '091373130', '131210048', '132651038', '061303227', '131323045', '132707035', '041941124', '131706041', '132773031', '021461121', '131206039', '062670128', '132638047', '133366036', '053750125', '133411043', '131450032', '031898122', '132413041', '111199130', '131197048', '132637038', '063339227', '133365045', '133410034', '053540123', '132906042', '131389030', '021371121', '131179039', '072647129', '132635047', '132725036', '052853225', '131746043', '132778032', '031206222', '132359041', '081183130', '131175048', '131611037', '061674227', '133749045', '131714033', '042668223', '132734042', '132350032', '033374120', '133222039', '073413128', '133402047', '133493035', '051493125', '131386043', '132669033', '041373122', '131325040', '082715130', '132709049', '132890037', '062922126', '132773045', '131370034', '042746123', '132651042', '131323031', '032855121', '131683038', '071843128', '131706046', '132773036', '051205225', '131206043', '132647033', '041358123', '133366040', '083734129', '133410048', '133498037', '061706226', '131389044', '131198034', '042717124', '132637042', '133349031', '023877120', '133410039', '073506128', '132906047', '131373036', '051243127', '131179044', '132635034', '043403123', '132725042', '092901130', '131746048', '132774038', '061398128', '132359046', '131175035', '041687125', '131355043', '131701031', '031957121', '131706039', '072730229', '132733047', '131326036', '053246126', '132710045', '133402033', '043658222', '133477041', '091453130', '131386048', '132669038', '061309228', '131325046', '132701035', '053349224', '132858043', ); function getLar($date, $isLunar = 1){//1为旧历0为新历 list($year, $month, $day) = split("-", $date); if($isLunar == 1) return $this->Lunar2Solar($year, $month, $day); else return $this->Solar2Lunar($year, $month, $day); } function IsLeapYear($AYear){ return ($AYear % 4 == 0) and (($AYear % 100 <> 0) or ($AYear % 400 == 0)); } function CovertLunarMonth($magicno){ $m = $magicno; for ($i = 12; $i >= 1; $i--){ $size = $m % 2; if ($size == 0) $this->LMDay[$i] = 29; else $this->LMDay[$i] = 30; $m = floor($m / 2); } } function ProcessMagicStr($yy){ $yy = $yy - 1583; $magicstr = $this->LongLife[$yy]; $this->InterMonth = substr($magicstr, 0, 2); $LunarMonth = substr($magicstr, 2, 4); $this->CovertLunarMonth($LunarMonth); $dsize = substr($magicstr, 6, 1); switch ($dsize) { case 0 : $this->InterMonthDays = 0; break; case 1 : $this->InterMonthDays = 29; break; case 2 : $this->InterMonthDays = 30; break; } $this->SLRangeDay = substr($magicstr, 7, 2); } function DaysPerLunarMonth($LYear, $LMonth){ $this->ProcessMagicStr($LYear); if ($LMonth < 0) return $this->InterMonthDays; else return $this->LMDay[$LMonth]; } function Solar2Lunar($SYear, $SMonth, $SDay){ if( !(1582 <= $SYear && $SYear <= 2135) ){ return false; } $day = 0; if ($this->isLeapYear($SYear)) $this->SMDay[2] = 29; $this->ProcessMagicStr($SYear); if ($SMonth == 1) $day = $SDay; else { for($i = 1; $i <= $SMonth-1; $i++) $day = $day + $this->SMDay[$i]; $day = $day + $SDay; } if ($day <= $this->SLRangeDay) { $day = $day - $this->SLRangeDay; $this->processmagicstr($SYear-1); for ($i = 12; $i >= 1; $i--){ $day = $day + $this->LMDay[$i]; if ($day > 0) break; } $LYear = $SYear - 1; $LMonth = $i; $LDay = $day; } else { $day = $day - $this->SLRangeDay; for($i = 1; $i <= $this->InterMonth-1; $i++){ $day = $day - $this->LMDay[$i]; if ($day <= 0) break; } if ($day <= 0) { $LYear = $SYear; $LMonth = $i; $LDay = $day + $this->LMDay[$i]; } else { $day = $day - $this->LMDay[$this->InterMonth]; if ($day <= 0) { $LYear = $SYear; $LMonth = $this->InterMonth; $LDay = $day + $this->LMDay[$this->InterMonth]; } else { $this->LMDay[$this->InterMonth] = $this->InterMonthDays; for ($i = $this->InterMonth; $i <= 12; $i++){ $day = $day - $this->LMDay[$i]; if ($day <= 0) break; } if ($i == $this->InterMonth) $LMonth = 0 - $this->InterMonth; else $LMonth = $i; $LYear = $SYear; $LDay = $day + $this->LMDay[$i]; } } } $dated = "$LYear-$LMonth-$LDay 00:00"; return $this->date_to_unixtime($dated); //return mktime(0, 0, 0, $LMonth, $LDay, $LYear); } function Lunar2Solar($LYear, $LMonth, $LDay){ if( !(1582 <= $LYear && $LYear <= 2135) ){ return false; } $day = 0; $SYear = $LYear; if ($this->isLeapYear($SYear)) $this->SMDay[2] = 29; $this->processmagicstr($SYear); if ($LMonth < 0) $day = $this->LMDay[$this->InterMonth]; if ($LMonth <> 1) for ($i = 1; $i <= $LMonth-1; $i++) $day = $day + $this->LMDay[$i]; $day = $day + $LDay + $this->SLRangeDay; if (($this->InterMonth <> 13) and ($this->InterMonth < $LMonth)) $day = $day + $this->InterMonthDays; for ($i = 1; $i <= 12; $i++){ $day = $day - $this->SMDay[$i]; if ($day <= 0) break; } if ($day > 0) { $SYear = $SYear + 1; if ($this->isLeapYear($SYear)) $this->SMDay[2] = 29; for ($i = 1; $i <= 12; $i++){ $day = $day - $this->SMDay[$i]; if ($day <= 0) break; } } $day = $day + $this->SMDay[$i]; $SMonth = $i; $SDay = $day; $dated = "$SYear-$SMonth-$SDay 00:00"; return $this->date_to_unixtime($dated); //return mktime(0, 0, 0, $SMonth, $SDay, $SYear); } // 计算生肖 function get_animal($birth_year){ //echo $birth_year; $animal = array( '鼠','牛','虎','兔','龙','蛇', '马','羊','猴','鸡','狗','猪' ); $my_animal = ($birth_year-1900)%12; return $animal[$my_animal]; } // 计算星座 function get_zodiac_sign($month, $day){ // 检查参数有效性 if ($month < 1 || $month > 12 || $day < 1 || $day > 31) return (false); // 星座名称以及开始日期 $signs = array( array( "20" => "水瓶座"), array( "19" => "双鱼座"), array( "21" => "白羊座"), array( "20" => "金牛座"), array( "21" => "双子座"), array( "22" => "巨蟹座"), array( "23" => "狮子座"), array( "23" => "处女座"), array( "23" => "天秤座"), array( "24" => "天蝎座"), array( "22" => "射手座"), array( "22" => "摩羯座") ); list($sign_start, $sign_name) = each($signs[(int)$month-1]); if ($day < $sign_start) list($sign_start, $sign_name) = each($signs[($month -2 < 0) ? $month = 11: $month -= 2]); return $sign_name; } // end of function //1970年以前和2038年以后的Unix时间戳转日期 先调用date_to_unixtime()函数 function unixtime_to_date_str($unixtime, $timezone = 'PRC') { $datetime = new DateTime("@$unixtime"); //DateTime类的bug,加入@可以将Unix时间戳作为参数传入 $datetime->setTimezone(new DateTimeZone($timezone)); return $datetime->format("Y年m月d日H时i分"); } //1970年以前和2038年以后的Unix时间戳转日期 先调用date_to_unixtime()函数 function unixtime_to_date($unixtime, $timezone = 'PRC') { $datetime = new DateTime("@$unixtime"); //DateTime类的bug,加入@可以将Unix时间戳作为参数传入 $datetime->setTimezone(new DateTimeZone($timezone)); return $datetime->format("Y-m-d H:i:s"); } //1970年以前和2038年以后的日期转Unix时间戳 function date_to_unixtime($date, $timezone = 'PRC') { $datetime= new DateTime($date, new DateTimeZone($timezone)); return $datetime->format('U'); } } // 测试 $lunar = new Lunar(); $date = $lunar->getLar('1900-1-7',1); // 计算公历 //echo date('Y-m-d', $date).'<br>'; echo $lunar->unixtime_to_date($date).'<br>'; echo $lunar->get_zodiac_sign(date('m',$date),date('d',$date)).'<br>'; // 计算星座 $date = $lunar->getLar('1900-1-7',0); // 计算农历 //echo date('Y-m-d', $date).'<br>'; echo $lunar->unixtime_to_date($date).'<br>'; echo $lunar->get_animal(date('Y',$date)).'<br>'; // 计算生肖 ?>
标签: php 计算阴历、阳历、属性、星座方法