php计算两个农历相差的时间包括有闰月
时间: 2023-06-24 15:06:29 浏览: 55
计算农历日期之间相差的天数,包括闰月,可以使用下面的 PHP 代码:
```php
/**
* 计算两个农历日期之间相差的天数,包括闰月
* @param array $lunar1 第一个农历日期数组,包括 year, month, day, isLeapMonth
* @param array $lunar2 第二个农历日期数组,包括 year, month, day, isLeapMonth
* @return int 相差的天数
*/
function lunarDiff($lunar1, $lunar2) {
$days1 = lunarToDays($lunar1['year'], $lunar1['month'], $lunar1['day'], $lunar1['isLeapMonth']);
$days2 = lunarToDays($lunar2['year'], $lunar2['month'], $lunar2['day'], $lunar2['isLeapMonth']);
return $days2 - $days1;
}
/**
* 将农历日期转换为距离公元1年1月1日的天数
* @param int $year 农历年份
* @param int $month 农历月份
* @param int $day 农历日期
* @param bool $isLeapMonth 是否为闰月
* @return int 距离公元1年1月1日的天数
*/
function lunarToDays($year, $month, $day, $isLeapMonth) {
// 农历每月的天数,包括闰月,0表示该年没有闰月
$lunarMonthDays = array(
1887 => array(30, 29, 30, 29, 30, 29, 30, 29, 30, 29, 30, 29),
1888 => array(30, 30, 29, 30, 29, 30, 29, 30, 29, 30, 29, 30),
// ... 其他年份的数据可以自行添加
);
// 农历每年的闰月月份,0表示该年没有闰月
$lunarLeapMonths = array(
1887 => 0,
1888 => 5,
// ... 其他年份的数据可以自行添加
);
$days = 0;
for ($y = 1887; $y < $year; $y++) {
$days += lunarYearDays($y);
}
$days += lunarMonthDays($year, $month, $isLeapMonth);
$days += $day - 1;
return $days;
}
/**
* 计算农历某年的天数
* @param int $year 农历年份
* @return int 该年的天数
*/
function lunarYearDays($year) {
$days = 0;
$months = $lunarMonthDays[$year];
foreach ($months as $monthDays) {
$days += $monthDays;
}
return $days;
}
/**
* 计算农历某年某月的天数
* @param int $year 农历年份
* @param int $month 农历月份
* @param bool $isLeapMonth 是否为闰月
* @return int 该月的天数
*/
function lunarMonthDays($year, $month, $isLeapMonth) {
$months = $lunarMonthDays[$year];
$monthDays = $months[$month - 1];
if ($isLeapMonth && $lunarLeapMonths[$year] == $month - 1) {
$monthDays += $months[$month];
}
return $monthDays;
}
```
这个代码中,`lunarDiff` 函数接受两个农历日期的数组参数,分别是年、月、日、是否为闰月。`lunarToDays` 函数将农历日期转换为距离公元1年1月1日的天数,`lunarYearDays` 函数计算某一年的天数,`lunarMonthDays` 函数计算某一月的天数。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)