如何 字四柱排盘中,流年、流月、流日的十神如何获取 php 源码
时间: 2023-12-24 20:02:08 浏览: 142
以下是 PHP 中获取八字四柱排盘中,流年、流月、流日的十神的示例代码:
```php
// 定义天干和地支的对应关系
$tiangan = array('甲', '乙', '丙', '丁', '戊', '己', '庚', '辛', '壬', '癸');
$dizhi = array('子', '丑', '寅', '卯', '辰', '巳', '午', '未', '申', '酉', '戌', '亥');
// 定义十神的对应关系
$shishen = array(
'比肩', '劫财', '食神', '伤官', '偏财', '正财', '七杀', '正官', '偏印', '正印'
);
// 获取出生年月日时的时间戳
$birthday = strtotime('1990-01-01 12:00:00');
// 计算出生时的八字
$bazi = calculate_bazi($birthday);
// 计算流年、流月和流日的干支
$current = time();
$year = date('Y', $current);
$month = date('n', $current);
$day = date('j', $current);
$hour = date('G', $current);
$lyear = get_ganzhi($year);
$lmonth = get_ganzhi_month($year, $month);
$lday = get_ganzhi_day($year, $month, $day, $hour);
// 确定流年、流月和流日的十神
$year_shishen = get_shishen($bazi['nian_gan'], $lyear['gan']);
$month_shishen = get_shishen($bazi['yue_gan'], $lmonth['gan'], $year_shishen);
$day_shishen = get_shishen($bazi['ri_gan'], $lday['gan'], $year_shishen);
// 输出结果
echo "出生八字:{$bazi['nian_gan']}{$bazi['nian_zhi']} {$bazi['yue_gan']}{$bazi['yue_zhi']} {$bazi['ri_gan']}{$bazi['ri_zhi']} {$bazi['shi_gan']}{$bazi['shi_zhi']}\n";
echo "流年干支:{$lyear['gan']}{$lyear['zhi']},十神:{$year_shishen}\n";
echo "流月干支:{$lmonth['gan']}{$lmonth['zhi']},十神:{$month_shishen}\n";
echo "流日干支:{$lday['gan']}{$lday['zhi']},十神:{$day_shishen}\n";
// 计算八字
function calculate_bazi($birthday)
{
$timestamp = $birthday;
$year = date('Y', $timestamp);
$month = date('n', $timestamp);
$day = date('j', $timestamp);
$hour = date('G', $timestamp);
// 计算年柱
$year_gan_index = ($year - 4) % 10;
$year_zhi_index = ($year - 4) % 12;
$nian_gan = $tiangan[$year_gan_index];
$nian_zhi = $dizhi[$year_zhi_index];
// 计算月柱
$month_gan_index = ($year_gan_index * 2 + $month - 2) % 10;
$month_zhi_index = ($month - 1) % 12;
$yue_gan = $tiangan[$month_gan_index];
$yue_zhi = $dizhi[$month_zhi_index];
// 计算日柱
$jd = get_julian_day($year, $month, $day) - 2440587.5;
$jd += ($hour - 12) / 24.0;
$wz = get_sun_longitude($jd);
$ri_zhi_index = intval(($wz + 15) / 30) % 12;
$ri_gan_index = ($year_gan_index * 5 + $ri_zhi_index + 1) % 10;
$ri_gan = $tiangan[$ri_gan_index];
$ri_zhi = $dizhi[$ri_zhi_index];
// 计算时柱
$shi_zhi_index = intval($hour / 2) % 12;
$shi_gan_index = ($ri_gan_index * 2 + $shi_zhi_index) % 10;
$shi_gan = $tiangan[$shi_gan_index];
$shi_zhi = $dizhi[$shi_zhi_index];
// 返回八字信息
return array(
'nian_gan' => $nian_gan,
'nian_zhi' => $nian_zhi,
'yue_gan' => $yue_gan,
'yue_zhi' => $yue_zhi,
'ri_gan' => $ri_gan,
'ri_zhi' => $ri_zhi,
'shi_gan' => $shi_gan,
'shi_zhi' => $shi_zhi,
);
}
// 计算干支
function get_ganzhi($year)
{
global $tiangan, $dizhi;
$gan_index = ($year - 4) % 10;
$zhi_index = ($year - 4) % 12;
return array('gan' => $tiangan[$gan_index], 'zhi' => $dizhi[$zhi_index]);
}
// 计算月干支
function get_ganzhi_month($year, $month)
{
global $tiangan, $dizhi;
$gan_index = ($year - 4) % 10;
$zhi_index = ($month - 1) % 12;
return array('gan' => $tiangan[$gan_index], 'zhi' => $dizhi[$zhi_index]);
}
// 计算日干支
function get_ganzhi_day($year, $month, $day, $hour)
{
global $tiangan, $dizhi;
$jd = get_julian_day($year, $month, $day) - 2440587.5;
$jd += ($hour - 12) / 24.0;
$wz = get_sun_longitude($jd);
$zhi_index = intval(($wz + 15) / 30) % 12;
$gan_index = ($year - 4) % 10 * 5 + $zhi_index + 1;
$gan_index %= 10;
return array('gan' => $tiangan[$gan_index], 'zhi' => $dizhi[$zhi_index]);
}
// 计算儒略日
function get_julian_day($year, $month, $day)
{
if ($month <= 2) {
$year--;
$month += 12;
}
$a = intval($year / 100);
$b = 2 - $a + intval($a / 4);
$jd = intval(365.25 * ($year + 4716)) + intval(30.6001 * ($month + 1)) + $day + $b - 1524.5;
return $jd;
}
// 计算太阳黄经
function get_sun_longitude($jd)
{
$t = ($jd - 2451545.0) / 36525;
$l = 280.46645 + 36000.76983 * $t + 0.0003032 * $t * $t;
$m = 357.52910 + 35999.05030 * $t - 0.0001559 * $t * $t - 0.00000048 * $t * $t * $t;
$e = 0.016708617 - 0.000042037 * $t - 0.0000001236 * $t * $t;
$y = tan(deg2rad($e / 2)) * tan(deg2rad($e / 2));
$v = $m + 1.914600 * sin(deg2rad($m)) + 0.019993 * sin(deg2rad(2 * $m));
$r = 1.000001018 * (1 - $y) / sqrt(1 + 0.006861 * (1 - $y) * sin(deg2rad($v)));
$xs = 125.04 - 1934.136 * $t;
$l += 0.00256 * cos(deg2rad($xs));
$l += 0.00015 * cos(deg2rad(2 * $m));
$l -= 0.00070 - 0.000000038 * $t * $t;
$z = $l - 0.00569 - 0.00478 * sin(deg2rad(125.04 - 1934.136 * $t));
$z += 0.00274 * sin(deg2rad(2 * $m - $xs));
return $z;
}
// 确定十神
function get_shishen($gan1, $gan2, $gan3 = '')
{
global $shishen;
$gan1_index = array_search($gan1, $tiangan);
$gan2_index = array_search($gan2, $tiangan);
$diff_index = ($gan2_index - $gan1_index + 10) % 10;
if ($gan3) {
$gan3_index = array_search($gan3, $tiangan);
$diff_index = ($gan3_index - $gan1_index + 10) % 10;
}
return $shishen[$diff_index];
}
```
这段代码实现了八字排盘、流年、流月、流日干支的计算,以及十神的确定。你可以根据自己的需求进行修改和完善。
阅读全文