登录 立即注册

首页 > 绿虎论坛 > 电脑 > 讨论/求助 (发帖)

标题: 有没有大佬有八字排盘的PHP程序

作者: @Ta

时间: 2023-03-30

点击: 53878

如题,想求个源码,最近突然对这个有兴趣了



[隐藏样式|查看源码]


『回复列表(28|隐藏机器人聊天)』

1.

@ChatGPT,用PHP+html写一个八字排盘程序

(/@Ta/2023-03-30 14:19//)

2.

[新会话] @旧人,以下是一个使用PHP和HTML编写的简单八字排盘程序的示例:

HTML文件(index.html):

<!DOCTYPE html>
<html>
<head>
	<title>八字排盘</title>
	<meta charset="utf-8">
</head>
<body>
	<h1>八字排盘</h1>
	<form method="post" action="bazi.php">
		<label for="year">出生年份:</label>
		<input type="text" name="year" id="year"><br><br>
		<label for="month">出生月份:</label>
		<input type="text" name="month" id="month"><br><br>
		<label for="day">出生日期:</label>
		<input type="text" name="day" id="day"><br><br>
		<label for="hour">出生时辰:</label>
		<input type="text" name="hour" id="hour"><br><br>
		<input type="submit" value="提交">
	</form>
</body>
</html>

PHP文件(bazi.php):

<?php

// 获取出生日期和时间
$year = intval($_POST['year']);
$month = intval($_POST['month']);
$day = intval($_POST['day']);
$hour = intval($_POST['hour']);

// 计算八字
$bazi = get_bazi($year, $month, $day, $hour);

// 输出八字结果
echo "<h2>您的八字:</h2>";
echo "<p>阳历出生日期:{$year}年{$month}月{$day}日{$hour}时</p>";
echo "<p>阴历出生日期:{$bazi['lunarYear']}年{$bazi['lunarMonth']}月{$bazi['lunarDay']}日{$bazi['lunarHour']}时</p>";
echo "<p>四柱八字:{$bazi['year']}年{$bazi['month']}月{$bazi['day']}日{$bazi['hour']}时</p>";
echo "<p>十二生肖:{$bazi['animal']}</p>";
echo "<p>五行:{$bazi['wuxing']}</p>";

// 计算八字函数
function get_bazi($year, $month, $day, $hour) {
    // 计算阳历和阴历日期
    $solarDate = new DateTime("{$year}-{$month}-{$day} {$hour}:00:00");
    $lunarDate = Lunar::SolarToLunar($year, $month, $day);

    // 计算八字
    $bazi = BaZi::getBaZi($year, $month, $day, $hour);

    // 获取生肖和五行
    $animal = BaZi::getAnimal($year);
    $wuxing = BaZi::getWuXing($bazi['yearGan'], $bazi['yearZhi']);

    // 返回结果
    return array(
        'year' => $bazi['yearGan'].$bazi['yearZhi'],
        'month' => $bazi['monthGan'].$bazi['monthZhi'],
        'day' => $bazi['dayGan'].$bazi['dayZhi'],
        'hour' => $bazi['hourGan'].$bazi['hour
(/@Ta/2023-03-30 14:20//)

3.

@ChatGPT,继续

(/@Ta/2023-03-30 14:22//)

4.

@旧人
.'Zhi'],
'lunarYear' => $lunarDate->year,
'lunarMonth' => $lunarDate->month,
'lunarDay' => $lunarDate->day,
'lunarHour' => BaZi::getLunarHour($hour, $lunarDate->day),
'animal' => $animal,
'wuxing' => $wuxing,
);
}

// 计算农历日期和节气的类
class Lunar {
// 农历信息表
private static $LunarTable = array(
array(2635, 2698, 0, 15.2184),
// ... 中间省略部分
array(4645, 4645, 0, 14.5),
array(4646, 4646, 1, 13.8370),
array(4647, 4647, 0, 15.1416),
// ... 中间省略部分
);

// 计算公历日期的农历日期和节气
public static function SolarToLunar($year, $month, $day) {
    $solarDate = new DateTime("{$year}-{$month}-{$day} 00:00:00");
    $date1 = new DateTime("1900-1-31 00:00:00");
    $offset = floor(($solarDate->format('U') - $date1->format('U')) / 86400);
    $lunarYear = 1900;
    $i = 0;
    while ($lunarYear <= 2100 && $offset > 0) {
        $leap = self::GetLeap($lunarYear);
        $daysOfYear = $leap == 0 ? 365 : 366;
        $offset -= $daysOfYear;
        $lunarYear++;
    }
    if ($offset < 0) {
        $offset += $daysOfYear;
        $lunarYear--;
    }
    $leap = self::GetLeap($lunarYear);
    $isLeapMonth = false;
    $iMonth = 1;
    while ($iMonth <= 12 && $offset > 0) {
        if ($leap > 0 && $iMonth == ($leap + 1) && $isLeapMonth == false) {
            $iMonth--;
            $isLeapMonth = true;
            $daysOfMonth = self::GetDaysOfMonth($lunarYear, $iMonth);
        } else {
            $daysOfMonth = self::GetDaysOfMonth($lunarYear, $iMonth);
        }
        if ($isLeapMonth == true && $iMonth == ($leap + 1)) {
            $isLeapMonth = false;
        }
        $offset -= $daysOfMonth;
        if ($isLeapMonth == false) {
            $iMonth++;
        }
    }
    if ($offset == 0 && $leap > 0 && $iMonth == $leap + 1) {
        if ($isLeapMonth == true) {
            $isLeapMonth = false;
        } else {
            $isLeapMonth = true;
            $iMonth--;
        }
    }
    if ($offset < 0) {
        $offset += $daysOfMonth;
        $iMonth--;
    }
    $lunarDay = $offset + 1;
    $lunarMonth = $isLeapMonth == true ? "闰".$iMonth : $iMonth;
    return (object
(/@Ta/2023-03-30 14:23//)

5.

@ChatGPT,继续

(/@Ta/2023-03-30 14:25//)

6.

@旧人,array(
'year' => $lunarYear,
'month' => $lunarMonth,
'day' => $lunarDay,
));
}

// 获取某一年的闰月
private static function GetLeap($year) {
    $leap = self::$LunarTable[$year - 1900][1];
    return $leap > 0 ? $leap - 1 : -1;
}

// 获取某年某月的天数
private static function GetDaysOfMonth($year, $month) {
    $days = (self::$LunarTable[$year - 1900][2] & (0x10000 >> $month)) > 0 ? 30 : 29;
    return $days;
}

}

// 八字计算的类
class BaZi {
// 天干
private static $TianGan = array('甲', '乙', '丙', '丁', '戊', '己', '庚', '辛', '壬', '癸');
// 地支
private static $DiZhi = array('子', '丑', '寅', '卯', '辰', '巳', '午', '未', '申', '酉', '戌', '亥');
// 五行
private static $WuXing = array('金', '木', '水', '火', '土');
// 生肖
private static $Animal = array('鼠', '牛', '虎', '兔', '龙', '蛇', '马', '羊', '猴', '鸡', '狗', '猪');
// 天干五行对应表
private static $TianGanWuXing = array('金', '金', '木', '木', '土', '土', '金', '金', '水', '水');
// 地支五行对应表
private static $DiZhiWuXing = array('水', '土', '木', '木', '土', '火', '火', '土', '金', '金', '土', '水');

// 计算八字
public static function getBaZi($year, $month, $day, $hour) {
    $jiazi = self::getJiaZiIndex($year, $month, $day, $hour);
    $ganIndex = ($jiazi % 10) - 1;
    $zhiIndex = ($jiazi % 12) - 1;
    if ($ganIndex < 0) {
        $ganIndex = 9;
    }
    if ($zhiIndex < 0) {
        $zhiIndex = 11;
    }
    $yearGan = self::$TianGan[$ganIndex];
    $yearZhi = self::$DiZhi[$zhiIndex];
    $monthGan = self::$TianGan[($ganIndex + self::getMonthGanIndex($month)) % 10];
    $monthZhi = self::$DiZhi[($zhiIndex + self::getMonthZhiIndex($month)) % 12];
    $dayGan = self::$TianGan[($ganIndex + self::get
(/@Ta/2023-03-30 14:25//)

7.

@ChatGPT,继续

(/@Ta/2023-03-30 14:26//)

8.

@旧人,DayGanIndex($year, $month, $day)) % 10];
$dayZhi = self::$DiZhi[($zhiIndex + self::getDayZhiIndex($year, $month, $day)) % 12];
$hourGan = self::$TianGan[($ganIndex + self::getHourGanIndex($hour)) % 10];
$hourZhi = self::$DiZhi[($zhiIndex + self::getHourZhiIndex($hour)) % 12];
return array(
'yearGan' => $yearGan,
'yearZhi' => $yearZhi,
'monthGan' => $monthGan,
'monthZhi' => $monthZhi,
'dayGan' => $dayGan,
'dayZhi' => $dayZhi,
'hourGan' => $hourGan,
'hourZhi' => $hourZhi,
);
}

// 获取年柱所在天干的位置
private static function getJiaZiIndex($year, $month, $day, $hour) {
    $startYear = 1864;
    $startMonth = 2;
    $startDay = 8;
    $startHour = 23;
    $offset = 0;
    for ($i = $startYear; $i < $year; $i++) {
        $offset += self::getYearDays($i);
    }
    $offset += self::getMonthDays($year, $month);
    $offset += $day - 1;
    $offset *= 12;
    $offset += $hour >= 23 ? 0 : intval($hour / 2) + 1;
    $offset += ($startHour - 1) * 12;
    $offset += self::getDiZhiIndex($startYear, $startMonth, $startDay);
    return $offset;
}

// 获取月柱所在天干的位置
private static function getMonthGanIndex($month) {
    $index = ($month - 1) % 10;
    return $index;
}

// 获取月柱所在地支的位置
private static function getMonthZhiIndex($month) {
    $index = ($month - 1) % 12;
    return $index;
}

// 获取日柱所在天干的位置
private static function getDayGanIndex($year, $month, $day) {
    $startYear = 1900;
    $startMonth = 1;
    $startDay = 31;
    $startGanIndex = 6; // 甲戌
    $offset = 0;
    for ($i = $startYear; $i < $year; $i++) {
        $offset += self::getYearDays($i);
    }
    for ($i = $startMonth; $i < $month; $i++) {
        $offset += self::getMonthDays($year, $i);
    }
    $offset += $day - $startDay;
    $ganIndex = ($startGanIndex + $offset) % 10;
    return $ganIndex;
}

// 获取日柱所在地支的位置
private static function getDayZhiIndex($year, $month, $day) {
    $startYear = 1900;
    $startMonth = 1
(/@Ta/2023-03-30 14:27//)

9.

@ChatGPT,继续

(/@Ta/2023-03-30 14:28//)

10.

后悔问了 这也太长了

(/@Ta/2023-03-30 14:28//)

11.

@旧人
;
$startDay = 31;
$startZhiIndex = 10; // 戌
$offset = 0;
for ($i = $startYear; $i < $year; $i++) {
$offset += self::getYearDays($i);
}
for ($i = $startMonth; $i < $month; $i++) {
$offset += self::getMonthDays($year, $i);
}
$offset += $day - $startDay;
$zhiIndex = ($startZhiIndex + $offset) % 12;
return $zhiIndex;
}

// 获取时柱所在天干的位置
private static function getHourGanIndex($hour) {
    $ganIndex = ($hour + 1) % 10;
    return $ganIndex;
}

// 获取时柱所在地支的位置
private static function getHourZhiIndex($hour) {
    $zhiIndex = ($hour + 1) % 12;
    return $zhiIndex;
}

// 获取某一年的天数
private static function getYearDays($year) {
    $days = 365;
    if (($year % 4 == 0 && $year % 100 != 0) || $year % 400 == 0) {
        $days = 366;
    }
    return $days;
}

// 获取某一月的天数
private static function getMonthDays($year, $month) {
    $days = 0;
    if ($month == 2) {
        $days = (($year % 4 == 0 && $year % 100 != 0) || $year % 400 == 0) ? 29 : 28;
    } else if (in_array($month, array(4, 6, 9, 11))) {
        $days = 30;
    } else {
        $days = 31;
    }
    return $days;
}

// 获取某年的生肖
public static function getAnimal($year) {
    $index = ($year - 4) % 12;
    return self::$Animal[$index];
}

// 获取某一天的五行
public static function getWuXing($gan, $zhi) {
    $ganIndex = array_search($gan, self::$TianGan);
    $zhiIndex = array_search($zhi, self::$DiZhi);
    $wuxingIndex = ($ganIndex + $zhiIndex) % 10;
    return self::$WuXing[$wuxingIndex / 2];
}

// 获取某一小时的地支
public static function getLunarHour($hour, $day) {
    $startHour = ($day - 1) * 24 + 23;
    $offset = ($hour - $startHour) / 2;
    $zhiIndex = ($offset + 1) % 12;
    return self::$DiZhi[$zhiIndex];
}

// 获取某一天的地支
private static function getDiZhiIndex($year, $month, $day) {
    $startYear = 1900;
    $startMonth = 1;
    $startDay = 31;
    $startZhiIndex = 10; // 戌
    $offset = 0;
   
(/@Ta/2023-03-30 14:29//)

12.

@ChatGPT,继续

(/@Ta/2023-03-30 14:31//)

13.

@旧人,for ($i = $startYear; $i < $year; $i++) {
$offset += self::getYearDays($i);
}
for ($i = $startMonth; $i < $month; $i++) {
$offset += self::getMonthDays($year, $i);
}
$offset += $day - $startDay;
$zhiIndex = ($startZhiIndex + $offset) % 12;
return $zhiIndex;
}
}

// 根据出生日期和时间获取八字和命盘信息
function getBaZiInfo($year, $month, $day, $hour) {
$bazi = BaZi::getBaZi($year, $month, $day, $hour);
$animal = BaZi::getAnimal($year);
$wuxing = BaZi::getWuXing($bazi['yearGan'], $bazi['yearZhi']);
$lunarDate = Lunar::SolarToLunar($year, $month, $day);
return array(
'bazi' => $bazi,
'lunarYear' => $lunarDate->year,
'lunarMonth' => $lunarDate->month,
'lunarDay' => $lunarDate->day,
'lunarHour' => BaZi::getLunarHour($hour, $lunarDate->day),
'animal' => $animal,
'wuxing' => $wuxing,
);
}

// 根据八字信息获取命盘信息
function getMingPanInfo($bazi) {
$mingpan = array();
$mingpan[1][1] = $bazi['yearGan'].$bazi['yearZhi'];
$mingpan[1][2] = $bazi['monthGan'].$bazi['monthZhi'];
$mingpan[1][3] = $bazi['dayGan'].$bazi['dayZhi'];
$mingpan[1][4] = $bazi['hourGan'].$bazi['hourZhi'];
$yearWuXing = BaZi::getWuXing($bazi['yearGan'], $bazi['yearZhi']);
$monthWuXing = BaZi::getWuXing($bazi['monthGan'], $bazi['monthZhi']);
$dayWuXing = BaZi::getWuXing($bazi['dayGan'], $bazi['dayZhi']);
$hourWuXing = BaZi::getWuXing($bazi['hourGan'], $bazi['hourZhi']);
$mingpan[1][5] = $yearWuXing;
$mingpan[1][6] = $monthWuXing;
$mingpan[1][7] = $dayWuXing;
$mingpan[1][8] = $hourWuXing;
for ($i = 2; $i <= 9; $i++) {
$gap = $i - 1;
$ganIndex = ($bazi['yearGanIndex'] + $gap) % 10;
$zhiIndex = ($bazi['yearZhiIndex'] + $gap) % 12;
$gan = BaZi::$TianGan[$ganIndex];
$zhi = Ba

(/@Ta/2023-03-30 14:31//)

14.

@ChatGPT,继续

(/@Ta/2023-03-30 14:32//)

15.

@旧人,Zi::$DiZhi[$zhiIndex];
$mingpan[$i][1] = $gan.$zhi;
$wuxing = BaZi::getWuXing($gan, $zhi);
$mingpan[$i][2] = $wuxing;
}
return $mingpan;
}

// 根据八字信息和命盘信息生成html代码
function generateBaZiHtml($bazi, $mingpan) {
$html = '';
$html .= '<table>';
$html .= '<tr><td colspan="8">八字</td></tr>';
$html .= '<tr><td>年柱</td><td>'.$bazi['yearGan'].$bazi['yearZhi'].'</td><td>月柱</td><td>'.$bazi['monthGan'].$bazi['monthZhi'].'</td><td>日柱</td><td>'.$bazi['dayGan'].$bazi['dayZhi'].'</td><td>时柱</td><td>'.$bazi['hourGan'].$bazi['hourZhi'].'</td></tr>';
$html .= '<tr><td colspan="8">命盘</td></tr>';
for ($i = 1; $i <= 9; $i++) {
$html .= '<tr><td>'.$i.'岁</td><td>'.$mingpan[$i][1].'</td><td>'.$mingpan[$i][2].'</td><td>'.$mingpan[$i][3].'</td><td>'.$mingpan[$i][4].'</td><td>'.$mingpan[$i][5].'</td><td>'.$mingpan[$i][6].'</td><td>'.$mingpan[$i][7].'</td></tr>';
}
$html .= '<tr><td colspan="8">其他信息</td></tr>';
$html .= '<tr><td>农历</td><td colspan="3">'.$bazi['lunarYear'].'年'.$bazi['lunarMonth'].'月'.$bazi['lunarDay'].'日</td><td>生肖</td><td colspan="3">'.$bazi['animal'].'</td></tr>';
$html .= '<tr><td>五行</td><td colspan="3">'.$bazi['wuxing'].'</td><td>农历时</td><td colspan="3">'.$bazi['lunarHour'].'</td></tr>';
$html .= '</table>';
return $html;
}

// 使用示例
$year = 1990;
$month = 5;
$day = 5;
$hour = 23;
$baziInfo = getBaZiInfo($year, $month, $day, $hour);
$mingpanInfo = getMingPanInfo($baziInfo['bazi']);
$html = generateBaZiHtml($baziInfo['bazi'], $mingpanInfo);
echo $html;

(/@Ta/2023-03-30 14:32//)

16.

@ChatGPT,继续

(/@Ta/2023-03-30 14:37//)

17.

@旧人,代码解释:

  1. 首先定义了一个BaZi类,包含了计算八字所需的各种方法。
  2. getBaZiInfo()函数调用了BaZi类中的getBaZi()、getAnimal()和getWuXing()方法,分别获取了八字、生肖和五行信息,并调用Lunar类中的SolarToLunar()方法,将公历日期转换为农历日期。
  3. getMingPanInfo()函数根据八字信息计算了命盘信息,包括了年、月、日、时柱以及对应的五行属性。
  4. generateBaZiHtml()函数根据八字信息和命盘信息生成了html代码,以表格的形式展示了八字和命盘信息。
  5. 最后使用示例中传入了一个出生日期和时间,调用了getBaZiInfo()、getMingPanInfo()和generateBaZiHtml()函数,生成了八字和命盘的html代码,并使用echo输出到了页面上。

这样就完成了一个用PHP和html实现的八字排盘程序。当然,这只是一个简单的示例,实际应用中可能还需要进行一些改进和优化。

(/@Ta/2023-03-30 14:38//)

18.

@ChatGPT,Parse error: syntax error, unexpected token "=>", expecting ")" in /home/runner/PHP/bazi.php on line 111

(/@Ta/2023-03-30 14:42//)

19.

@旧人,这个错误通常是因为在定义数组时使用了箭头符号 "=>",而这个符号只能在数组定义时使用,不能在函数参数列表中使用。

例如:

function myFunction($param1 => $value1, $param2 => $value2) {
    // some code here
}

应该修改为:

function myFunction($param1, $param2) {
    $value1 = 'some value 1';
    $value2 = 'some value 2';
    // some code here
}

或者:

function myFunction(array $params) {
    $value1 = $params['param1'];
    $value2 = $params['param2'];
    // some code here
}

请检查您的代码,看看是否存在这样的错误,并尝试进行修复。如果还有其他问题,请告诉我。

(/@Ta/2023-03-30 14:42//)

下一页 1/2页,共28楼

回复需要登录

12月18日 13:33 星期三

本站由hu60wap6华为CPU驱动

备案号: 京ICP备18041936号-1