php农历时间函数

农历时间是中国传统文化中的一个重要组成部分,其使用时间与日常生活密切相关。为了方便程序员在PHP中对农历时间进行处理,本文介绍了几个常用的PHP农历时间函数。

一、PHP农历转阳历函数

以下是一个将农历转换成阳历的PHP函数:

```php

function lunarToSolar($lunarYear, $lunarMonth, $lunarDate) {

$daysInLunarMonth = array(

29, 30, 29, 30, 29, 30, 29, 30, 29, 30, 29, 30

);

$lunarMonth = intval($lunarMonth);

$lunarYear = intval($lunarYear);

$offset = $lunarYear - 1930;

$springOffset = array(3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 1, 2);

$differentDays = array(

array(13, 13, 13, 13, 13, 13, 12, 12, 12, 12, 12, 12),

array(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0),

array(31, 31, 31, 31, 31, 30, 30, 29, 29, 29, 29, 29),

array(59, 59, 59, 58, 58, 58, 57, 56, 56, 56, 56, 55),

array(90, 90, 90, 89, 89, 89, 88, 87, 87, 87, 86, 86),

array(120, 120, 120, 120, 120, 119, 119, 118, 118, 118, 117, 116),

array(151, 151, 151, 151, 151, 151, 150, 149, 149, 149, 148, 147),

array(181, 181, 181, 181, 181, 181, 181, 180, 180, 179, 178, 178),

array(212, 212, 212, 212, 212, 212, 211, 211, 210, 209, 209, 209),

array(243, 243, 243, 243, 243, 243, 243, 242, 241, 241, 240, 239),

array(273, 273, 273, 273, 273, 273, 273, 273, 272, 271, 271, 270),

array(304, 304, 304, 304, 304, 304, 304, 304, 303, 302, 302, 301),

array(334, 335, 335, 335, 335, 334, 334, 333, 333, 332, 332, 331)

);

$springDate = array(

2, 21, 21, 22, 21, 21, 22, 22, 23, 23, 22, 22

);

$daysFromSpringFestival = array(0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334);

$daysInSolarMonth = array(

31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31

);

$daysFromSpring = 0;

$leapMonth = ($differentDays[$offset][$lunarMonth - 1] == 0) ? 0 : ($differentDays[$offset][12] == $daysInLunarMonth[$lunarMonth - 1] ? intval($differentDays[$offset][$lunarMonth - 1] / 29) : 12);

for ($i = 0; $i < intval($lunarMonth) - 1; $i++) {

$daysFromSpring += $daysInLunarMonth[$i];

}

$daysFromSpring += $differentDays[$offset][$lunarMonth - 1];

if ($leapMonth != 0 && $lunarMonth > $leapMonth) {

$daysFromSpring += $daysInLunarMonth[$leapMonth - 1];

}

if ($springOffset[$offset] > $lunarMonth) {

$daysFromSpring += $daysFromSpringFestival[$offset];

}

$solarDate = $springDate[$offset] + $daysFromSpring - 1;

if ($leapMonth == intval($lunarMonth) && $differentDays[$offset][intval($lunarMonth) - 1] != 0 && $lunarDate > $differentDays[$offset][intval($lunarMonth) - 1]) {

$solarDate += $daysInSolarMonth[intval($lunarMonth) - 1] + 1;

$solarDate += $lunarDate - $differentDays[$offset][intval($lunarMonth) - 1];

} else {

$solarDate += $daysInSolarMonth[intval($lunarMonth) - 1] - 1;

$solarDate += $lunarDate;

}

return date("Y-m-d", strtotime(($lunarYear - 1900) . "year + " . ($solarDate - 1) . "day"));

}

```

该函数接收一个 $lunarYear(农历年份)、$lunarMonth(农历月份) 和 $lunarDate(农历日) 的参数,以及一个阳历日期字符串。它会返回一个阳历日期字符串,其中包含转换后的阳历日期。

二、PHP阳历转农历函数

以下是一个将阳历日期转换成农历日期的PHP函数:

```php

function solarToLunar($solarYear, $solarMonth, $solarDate) {

$lunarYear = 0;

$lunarMonth = 0;

$lunarDay = 0;

$daysInLunarMonth = array(

29, 30, 29, 30, 29, 30, 29, 30, 29, 30, 29, 30

);

$daysFromSpringFestival = array(0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334);

$daysInSolarMonth = array(

31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31

);

$differenceInDays = array(

array(0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334),

array(31, 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305),

array(59, 31, 0, 31, 61, 92, 122, 153, 183, 214, 245, 275),

array(90, 60, 31, 0, 31, 62, 93, 123, 154, 184, 215, 246),

array(120, 91, 61, 31, 0, 31, 63, 94, 124, 155, 185, 216),

array(151, 121, 92, 62, 31, 0, 31, 64, 95, 125, 156, 186),

array(181, 152, 122, 93, 63, 31, 0, 31, 65, 96, 126, 157),

array(212, 182, 153, 123, 94, 64, 31, 0, 31, 66, 97, 127),

array(243, 213, 183, 154, 124, 95, 65, 31, 0, 31, 67, 98),

array(273, 244, 214, 184, 155, 125, 96, 66, 31, 0, 31, 68),

array(304, 274, 245, 215, 185, 156, 126, 97, 67, 31, 0, 31),

array(334, 305, 275, 246, 216, 186, 157, 127, 98, 68, 31, 0)

);

if ($solarMonth == 2 && $solarDate == 29) {

return false;

}

$daysBetween = (mktime(0, 0, 0, $solarMonth, $solarDate, $solarYear) - strtotime("1900-1-31")) / 86400;

for ($i = 1; $i <= 12; $i++) {

if ($daysBetween < $daysFromSpringFestival[$i] + $differenceInDays[intval($i) - 1][(int)($lunarYear > 0 && $daysInLunarMonth[intval($i) - 1] == 30)]) {

$lunarMonth = $i;

$lunarDay = $daysBetween - $daysFromSpringFestival[$i - 1] - $differenceInDays[intval($i) - 1][(int)($lunarYear > 0 && $daysInLunarMonth[intval($i) - 1] == 30)] + 1;

break;

}

}

$t = mktime(0, 0, 0, $lunarMonth, $lunarDay, $solarYear);

$lunarYear = date("Y", $t);

return array("lunarYear" => $lunarYear, "lunarMonth" => $lunarMonth, "lunarDay" => $lunarDay);

}

```

该函数接受一个阳历日期参数,分别是 $solarYear(阳历年份)、$solarMonth(阳历月份) 和 $solarDate(阳历日)。它返回一个包含农历年、月和日的关联数组。

三、PHP农历日期转换

以下是一个将农历日期字符串转换成农历日期的PHP函数:

```php

function lunarDateStr($dateStr) {

$year = date("Y", strtotime($dateStr));

$month = date("n", strtotime($dateStr));

$day = date("j", strtotime($dateStr));

$lunarDate = solarToLunar($year, $month, $day);

$lunarYear = $lunarDate["lunarYear"];

$lunarMonth = $lunarDate["lunarMonth"];

$lunarDay = $lunarDate["lunarDay"];

$lunarMonth = $this->lunarMonthConvert($lunarMonth, true);

$lunarDay = $this->lunarMonthConvert($lunarDay, false);

$lunarYear = $this->getLunarYear($lunarYear);

$lunarDateStr = $lunarYear . "年" . $lunarMonth . "月" . $lunarDay . "日";

return $lunarDateStr;

}

function lunarMonthConvert($month, $isMonth) {

$monthChars = array(

"〇",

"一",

"二",

"三",

"四",

"五",

"六",

"七",

"八",

"九",

"十",

"冬",

"腊"

);

if ($isMonth == true) {

if ($month < 1 || $month > 12) {

return "";

}

if ($month < 11) {

$monthStr = $monthChars[intval($month)];

} elseif ($month == 11) {

$monthStr = "十一";

} else {

$monthStr = "十二";

}

$monthStr .= "月";

return $monthStr;

} else {

if ($month < 1 || $month > 30) {

return "";

}

if ($month < 11) {

$dayStr = "初" . $monthChars[intval($month)];

} elseif ($month == 20) {

$dayStr = "二十";

} elseif ($month == 30) {

$dayStr = "三十";

} else {

$dayStr = "廿" . $monthChars[intval($month) - 10];

}

return $dayStr;

}

}

function getLunarYear($year) {

$heavenlyStems = array(

"甲",

"乙",

"丙",

"丁",

"戊",

"己",

"庚",

"辛",

"壬",

"癸"

);

$earthlyBranches = array(

"子",

"丑",

"寅",

"卯",

"辰",

"巳",

"午",

"未",

"申",

"酉",

"戌",

"亥"

);

$year = $year - 1900 + 36;

$heavenlyStemIndex = $year % 10;

$earthlyBranchIndex = $year % 12;

return $heavenlyStems[$heavenlyStemIndex] . $earthlyBranches[$earthlyBranchIndex];

}

```

该函数接收一个农历日期字符串,并返回一个包含中文格式的农历日期的字符串。

结尾延伸说明:

以上介绍了几个在PHP中处理农历时间的函数。使用这些函数,您可以轻松地在您的PHP应用程序中转换农历日期和阳历日期,以及格式化您的日期字符串。但是,需要注意的是,跨度太大的日期转换会有些不准确,因为历法制定之初就有一些差错和误差。

壹涵网络我们是一家专注于网站建设、企业营销、网站关键词排名、AI内容生成、新媒体营销和短视频营销等业务的公司。我们拥有一支优秀的团队,专门致力于为客户提供优质的服务。

我们致力于为客户提供一站式的互联网营销服务,帮助客户在激烈的市场竞争中获得更大的优势和发展机会!

点赞(97) 打赏

评论列表 共有 0 条评论

暂无评论
立即
投稿
发表
评论
返回
顶部