有没有大佬有八字排盘的PHP程序
@旧人,
// 计算公历年月日对应的农历年月日
public static function SolarToLunar($year, $month, $day) {
if ($year < 1900 || $year > 2100) {
return false;
}
if ($month < 1 || $month > 12) {
return false;
}
if ($day < 1 || $day > 31) {
return false;
}
$offset = (strtotime("{$year}-{$month}-{$day}") - strtotime("1900-1-31")) / 86400;
$dayCyl = $offset + 40;
$monCyl = 14;
for ($i = 1900; $i < 2101 && $offset > 0; $i++) {
$daysInYear = self::getDaysInLunarYear($i);
$offset -= $daysInYear;
$monCyl += 12;
}
if ($offset < 0) {
$offset += $daysInYear;
$i--;
$monCyl -= 12;
}
$year = $i;
$yearCyl = $i - 1864;
$leapMonth = self::getLeapMonth($i);
$isLeap = false;
for ($i = 1; $i < 13 && $offset > 0; $i++) {
if ($leapMonth > 0 && $i == ($leapMonth + 1) && !$isLeap) {
$i--;
$isLeap = true;
$daysInMonth = self::getDaysInLeapMonth($year);
} else {
$daysInMonth = self::getDaysInLunarMonth($year, $i);
}
if ($isLeap && $i == ($leapMonth + 1)) {
$isLeap = false;
}
$offset -= $daysInMonth;
if (!$isLeap) {
$monCyl++;
}
}
if ($offset == 0 && $leapMonth > 0 && $i == $leapMonth + 1) {
if ($isLeap) {
$isLeap = false;
} else {
$isLeap = true;
$i--;
$monCyl--;
}
}
if ($offset < 0) {
$offset += $daysInMonth;
$i--;
$monCyl--;
}
$month = $i;
$day = $offset + 1;
if ($leapMonth > 0 && $month > $leapMonth) {
$month--;
if ($month == $leapMonth) {
if ($isLeap) {
$isLeap = false;
} else {
$isLeap = true;
$month++;
}
}
}
if ($isLeap) {
$lunarMonth = self::$LunarMonths[$month - 1].'闰';
} else {
$lunarMonth = self::$LunarMonths[$month - 1];
}
$lunarDay = self::getLunarDay($day);
return (object) [
'year' => $year,
'month' => $month,
'day' => $day,
'isLeap' => $isLeap,
'lunarYear' => self::getLunarYearName($year),
'lunarMonth' => $lunarMonth,
'l
ChatGPT写的八字php程序,修复了一些运行的错误,不过我测试了一下农历好像算的不太准确,其他的也看不懂,有人有兴趣再改改吧
chatgpt八字.zip(4.6 KB)
@ChatGPT,出错的代码如下return (object [
'year' => $lunarYear,
'month' => $lunarMonth,
'day' => $lunarDay,
]);