最近正好在开发这个生辰八字的,之前用的一套代码,发现计算出来的生辰八字不对,于是在网上找了一个新的方法,但是这个方法在计算月柱的时候不对,于是又是一顿百度搜索,可惜这块的很少,还好最后还是找到了一个人改良版的,将月柱计算的bug问题修复了。下面是完整的代码,大家可以参考下。
class Sizhu { public $niangan,$nianzhi,$yuegan,$rigan; function nianzhu($year) { $a = array('甲','乙','丙','丁','戊','己','庚','辛','壬','癸'); $b = array('子','丑','寅','卯','辰','巳','午','未','申','酉','戌','亥'); $last = $year % 10; if($last <= 3) { $last += 10; } $this->niangan = $last - 3; $tiangan = $a[$this->niangan - 1]; $last = $year % 100; if($year >= 1800 && $year <= 1899) { $nianzhi = $last + 9; } else if($year >= 1900 && $year <= 1999) { $nianzhi = $last + 1; } else if($year >= 2000 && $year <= 2099) { $nianzhi = $last + 5; } if($nianzhi > 12) { $nianzhi %= 12; } $this->nianzhi = $nianzhi; $dizhi = $b[$nianzhi - 1]; return $tiangan . $dizhi; } function yuezhu($year, $month){ $this->yuegan = ($year - 1900) * 12 + $month + 12; $a = array('甲','乙','丙','丁','戊','己','庚','辛','壬','癸'); $b = array("子", "丑", "寅", "卯", "辰", "巳", "午", "未", "申", "酉", "戌", "亥"); return $a[($this->yuegan % 10) - 1] . $b[($this->yuegan % 12) - 1]; } function rizhu($year,$month,$day) { $a = array('甲','乙','丙','丁','戊','己','庚','辛','壬','癸'); $b = array('子','丑','寅','卯','辰','巳','午','未','申','酉','戌','亥'); $today = strtotime("{$year}-{$month}-{$day}"); $year_start = strtotime("{$year}-01-01"); $days = ( $today - $year_start )/86400 + 1; $n = (int)(($year - 1900) * 5 + ($year - 1900 + 3) / 4 + 9 + $days); $n = $n % 60; $this->rigan = $n % 10; if($this->rigan == 0) { $this->rigan = 10; } $dizhi = $n % 12; if($dizhi == 0) { $dizhi = 12; } return $a[$this->rigan - 1] . $b[$dizhi - 1]; } function shizhu($hour) { $a = array('甲','乙','丙','丁','戊','己','庚','辛','壬','癸'); $b = array('子','丑','寅','卯','辰','巳','午','未','申','酉','戌','亥'); if($hour >= 23 || $hour < 1) { $shizhi = 1; } else if($hour >= 1 && $hour < 3) { $shizhi = 2; } else if($hour >= 3 && $hour < 5) { $shizhi = 3; } else if($hour >= 5 && $hour < 7) { $shizhi = 4; } else if($hour >= 7 && $hour < 9) { $shizhi = 5; } else if($hour >= 9 && $hour < 11) { $shizhi = 6; } else if($hour >= 11 && $hour < 13) { $shizhi = 7; } else if($hour >= 13 && $hour < 15) { $shizhi = 8; } else if($hour >= 15 && $hour < 17) { $shizhi = 9; } else if($hour >= 17 && $hour < 19) { $shizhi = 10; } else if($hour >= 19 && $hour < 21) { $shizhi = 11; } else if($hour >= 21 && $hour < 23) { $shizhi = 12; } $n = $this->rigan * 2 + $shizhi - 2; if($n > 10) { $n %= 10; } return $a[$n - 1] . $b[$shizhi - 1]; } public function test(){ $a= $this->rizhu(2049, 11, 2); var_dump($a); } }
好了,今天就给大家分享到这里。 秋峰,phper、javascript前端开发,目前自创业,做项目系统开发,做公众号以及小程序等的开发, 今天就给大家分享到这里,希望对大家有所帮助。
欢迎交流 大家相互学习 我的 微信号 qiufeng2983