带非正式节日 function rili() { global $solar, $dateInput; $date = getValidDate(); if (!$solar) { $solar = Solar::fromYmd((int)date('Y'), (int)date('m'), (int)date('d')); } $year = substr($date, 0, 4) ?: date('Y'); $month = substr($date, 4, 2) ?: date('m'); $days = substr($date, 6, 2) ?: date('d'); try { $solar = Solar::fromYmd($year, $month, $days); } catch (Exception $e) { $solar = Solar::fromYmd((int)date('Y'), (int)date('m'), (int)date('d')); } $state = [ 'year' => $year, 'month' => $month, 'weekStart' => 1, 'heads' => ['一', '二', '三', '四', '五', '六', '日'], 'weeks' => [] ]; $state = render($state); $calendarHtml = ''; $calendarHtml .= '
'; foreach ($state['heads'] as $index => $head) { if ($index >= 5) { $calendarHtml .= '
' . $head . '
'; } else { $calendarHtml .= '
' . $head . '
'; } } $calendarHtml .= '
'; $calendarHtml .= '
'; foreach ($state['weeks'] as $week) { foreach ($week->days as $day) { $classes = []; if ($day->isToday) { $classes[] = 'dtdaycss'; } if ($day->isOther) { $classes[] = 'fdy'; } if ($day->isRest) { $classes[] = 'jiarixiu'; } if ($day->isWeekend) { $classes[] = 'zhoumo'; } if ($day->isOvertime) { $classes[] = 'jiariban'; } $classString = implode(' ', $classes); $dayUrl = "/wnl/{$day->year}" . str_pad($day->month, 2, '0', STR_PAD_LEFT) . str_pad($day->day, 2, '0', STR_PAD_LEFT) . ".html"; $title = "{$day->year}年{$day->month}月{$day->day}日,农历" . $day->textmonth .'月'. $day->textday; // 关键修改:获取当前日期的非正式节日 $otherFestivals = []; try { // 1. 初始化当前日期的公历对象 $daySolar = Solar::fromYmd($day->year, $day->month, $day->day); // 2. 获取公历非正式节日(如纪念日) $gongliOther = $daySolar->getOtherFestivals() ?: []; // 3. 转换为农历对象 $dayLunar = $daySolar->getLunar(); // 4. 获取农历非正式节日 $nongliOther = $dayLunar->getOtherFestivals() ?: []; // 5. 合并去重 $otherFestivals = array_unique(array_merge($gongliOther, $nongliOther)); } catch (Exception $e) { // 日期无效时不显示节日(避免报错) $otherFestivals = []; } // 组合农历日期和非正式节日(格式:农历日期 + 节日,如有) $nldayContent = $day->text; // 原农历日期(如“正月初一”) if (!empty($otherFestivals)) { $nldayContent .= '
' . implode(',', $otherFestivals); // 换行显示节日 } $calendarHtml .= '
'; if ($day->isHoliday) { if ($day->isRest) { $calendarHtml .= ''; } else { $calendarHtml .= ''; } } else { if ($day->isRest) { $calendarHtml .= ''; } else { $calendarHtml .= ''; } } $calendarHtml .= ''; $calendarHtml .= '' . $day->day . ''; if ($day->isReplacedText) { $calendarHtml .= '' . $nldayContent . ''; } else { $calendarHtml .= '' . $nldayContent . ''; // 输出合并后的内容 } $calendarHtml .= ''; $calendarHtml .= '
'; } } $calendarHtml .= '
'; return $calendarHtml; }