PHP计算四柱的代码网上应该是没有的,这是宇润花了好几天时间研究的成果!
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($month)
{
$a = array('甲','乙','丙','丁','戊','己','庚','辛','壬','癸');
$b = array('寅','卯','辰','巳','午','未','申','酉','戌','亥','子','丑');
$this->yuegan = $this->niangan * 2 + $month;
if($this->yuegan > 10)
{
$this->yuegan %= 10;
}
return $a[$this->yuegan - 1] . $b[$month - 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
{
$shizhi = 1;
}
else if($hour >= 1 && $hour
{
$shizhi = 2;
}
else if($hour >= 3 && $hour
{
$shizhi = 3;
}
else if($hour >= 5 && $hour
{
$shizhi = 4;
}
else if($hour >= 7 && $hour
{
$shizhi = 5;
}
else if($hour >= 9 && $hour
{
$shizhi = 6;
}
else if($hour >= 11 && $hour
{
$shizhi = 7;
}
else if($hour >= 13 && $hour
{
$shizhi = 8;
}
else if($hour >= 15 && $hour
{
$shizhi = 9;
}
else if($hour >= 17 && $hour
{
$shizhi = 10;
}
else if($hour >= 19 && $hour
{
$shizhi = 11;
}
else if($hour >= 21 && $hour
{
$shizhi = 12;
}
$n = $this->rigan * 2 + $shizhi - 2;
if($n > 10)
{
$n %= 10;
}
return $a[$n - 1] . $b[$shizhi - 1];
}
}
年柱和月柱都需要传农历的年和月,日柱和时柱都是公历的几号和几时。
不要问我为什么这么算,百度找来的计算方式,然后尝试修改而成,我并不懂原理。
跟其它网站以及易语言内置的四柱计算函数的计算结果比较过一些日期,目前还未发现有问题。