1. php 获取当月的阴历值
<?php function lunarcalendar($month, $year) { global $lnlunarcalendar; //农历每月的天数。每个元素为一年。每个元素中的数据为:[0]是闰月在哪个月,0为无闰月;[1]到[13]是每年12或13个月的每月天数;[14]是当年的天干次序,[15]是当年的地支次序 $everymonth = array(0 => array(8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 29, 30, 7, 1), 1 => array(0, 29, 30, 29, 29, 30, 29, 30, 29, 30, 30, 30, 29, 0, 8, 2), 2 => array(0, 30, 29, 30, 29, 29, 30, 29, 30, 29, 30, 30, 30, 0, 9, 3), 3 => array(5, 29, 30, 29, 30, 29, 29, 30, 29, 29, 30, 30, 29, 30, 10, 4), 4 => array(0, 30, 30, 29, 30, 29, 29, 30, 29, 29, 30, 30, 29, 0, 1, 5), 5 => array(0, 30, 30, 29, 30, 30, 29, 29, 30, 29, 30, 29, 30, 0, 2, 6), 6 => array(4, 29, 30, 30, 29, 30, 29, 30, 29, 30, 29, 30, 29, 30, 3, 7), 7 => array(0, 29, 30, 29, 30, 29, 30, 30, 29, 30, 29, 30, 29, 0, 4, 8), 8 => array(0, 30, 29, 29, 30, 30, 29, 30, 29, 30, 30, 29, 30, 0, 5, 9), 9 => array(2, 29, 30, 29, 29, 30, 29, 30, 29, 30, 30, 30, 29, 30, 6, 10), 10 => array(0, 29, 30, 29, 29, 30, 29, 30, 29, 30, 30, 30, 29, 0, 7, 11), 11 => array(6, 30, 29, 30, 29, 29, 30, 29, 29, 30, 30, 29, 30, 30, 8, 12), 12 => array(0, 30, 29, 30, 29, 29, 30, 29, 29, 30, 30, 29, 30, 0, 9, 1), 13 => array(0, 30, 30, 29, 30, 29, 29, 30, 29, 29, 30, 29, 30, 0, 10, 2), 14 => array(5, 30, 30, 29, 30, 29, 30, 29, 30, 29, 30, 29, 29, 30, 1, 3), 15 => array(0, 30, 29, 30, 30, 29, 30, 29, 30, 29, 30, 29, 30, 0, 2, 4), 16 => array(0, 29, 30, 29, 30, 29, 30, 30, 29, 30, 29, 30, 29, 0, 3, 5), 17 => array(2, 30, 29, 29, 30, 29, 30, 30, 29, 30, 30, 29, 30, 29, 4, 6), 18 => array(0, 30, 29, 29, 30, 29, 30, 29, 30, 30, 29, 30, 30, 0, 5, 7), 19 => array(7, 29, 30, 29, 29, 30, 29, 29, 30, 30, 29, 30, 30, 30, 6, 8), 20 => array(0, 29, 30, 29, 29, 30, 29, 29, 30, 30, 29, 30, 30, 0, 7, 9), 21 => array(0, 30, 29, 30, 29, 29, 30, 29, 29, 30, 29, 30, 30, 0, 8, 10), 22 => array(5, 30, 29, 30, 30, 29, 29, 30, 29, 29, 30, 29, 30, 30, 9, 11), 23 => array(0, 29, 30, 30, 29, 30, 29, 30, 29, 29, 30, 29, 30, 0, 10, 12), 24 => array(0, 29, 30, 30, 29, 30, 30, 29, 30, 29, 30, 29, 29, 0, 1, 1), 25 => array(4, 30, 29, 30, 29, 30, 30, 29, 30, 30, 29, 30, 29, 30, 2, 2), 26 => array(0, 29, 29, 30, 29, 30, 29, 30, 30, 29, 30, 30, 29, 0, 3, 3), 27 => array(0, 30, 29, 29, 30, 29, 30, 29, 30, 29, 30, 30, 30, 0, 4, 4), 28 => array(2, 29, 30, 29, 29, 30, 29, 29, 30, 29, 30, 30, 30, 30, 5, 5), 29 => array(0, 29, 30, 29, 29, 30, 29, 29, 30, 29, 30, 30, 30, 0, 6, 6), 30 => array(6, 29, 30, 30, 29, 29, 30, 29, 29, 30, 29, 30, 30, 29, 7, 7), 31 => array(0, 30, 30, 29, 30, 29, 30, 29, 29, 30, 29, 30, 29, 0, 8, 8), 32 => array(0, 30, 30, 30, 29, 30, 29, 30, 29, 29, 30, 29, 30, 0, 9, 9), 33 => array(5, 29, 30, 30, 29, 30, 30, 29, 30, 29, 30, 29, 29, 30, 10, 10), 34 => array(0, 29, 30, 29, 30, 30, 29, 30, 29, 30, 30, 29, 30, 0, 1, 11), 35 => array(0, 29, 29, 30, 29, 30, 29, 30, 30, 29, 30, 30, 29, 0, 2, 12), 36 => array(3, 30, 29, 29, 30, 29, 29, 30, 30, 29, 30, 30, 30, 29, 3, 1), 37 => array(0, 30, 29, 29, 30, 29, 29, 30, 29, 30, 30, 30, 29, 0, 4, 2), 38 => array(7, 30, 30, 29, 29, 30, 29, 29, 30, 29, 30, 30, 29, 30, 5, 3), 39 => array(0, 30, 30, 29, 29, 30, 29, 29, 30, 29, 30, 29, 30, 0, 6, 4), 40 => array(0, 30, 30, 29, 30, 29, 30, 29, 29, 30, 29, 30, 29, 0, 7, 5), 41 => array(6, 30, 30, 29, 30, 30, 29, 30, 29, 29, 30, 29, 30, 29, 8, 6), 42 => array(0, 30, 29, 30, 30, 29, 30, 29, 30, 29, 30, 29, 30, 0, 9, 7), 43 => array(0, 29, 30, 29, 30, 29, 30, 30, 29, 30, 29, 30, 29, 0, 10, 8), 44 => array(4, 30, 29, 30, 29, 30, 29, 30, 29, 30, 30, 29, 30, 30, 1, 9), 45 => array(0, 29, 29, 30, 29, 29, 30, 29, 30, 30, 30, 29, 30, 0, 2, 10), 46 => array(0, 30, 29, 29, 30, 29, 29, 30, 29, 30, 30, 29, 30, 0, 3, 11), 47 => array(2, 30, 30, 29, 29, 30, 29, 29, 30, 29, 30, 29, 30, 30, 4, 12), 48 => array(0, 30, 29, 30, 29, 30, 29, 29, 30, 29, 30, 29, 30, 0, 5, 1), 49 => array(7, 30, 29, 30, 30, 29, 30, 29, 29, 30, 29, 30, 29, 30, 6, 2), 50 => array(0, 29, 30, 30, 29, 30, 30, 29, 29, 30, 29, 30, 29, 0, 7, 3), 51 => array(0, 30, 29, 30, 30, 29, 30, 29, 30, 29, 30, 29, 30, 0, 8, 4), 52 => array(5, 29, 30, 29, 30, 29, 30, 29, 30, 30, 29, 30, 29, 30, 9, 5), 53 => array(0, 29, 30, 29, 29, 30, 30, 29, 30, 30, 29, 30, 29, 0, 10, 6), 54 => array(0, 30, 29, 30, 29, 29, 30, 29, 30, 30, 29, 30, 30, 0, 1, 7), 55 => array(3, 29, 30, 29, 30, 29, 29, 30, 29, 30, 29, 30, 30, 30, 2, 8), 56 => array(0, 29, 30, 29, 30, 29, 29, 30, 29, 30, 29, 30, 30, 0, 3, 9), 57 => array(8, 30, 29, 30, 29, 30, 29, 29, 30, 29, 30, 29, 30, 29, 4, 10), 58 => array(0, 30, 30, 30, 29, 30, 29, 29, 30, 29, 30, 29, 30, 0, 5, 11), 59 => array(0, 29, 30, 30, 29, 30, 29, 30, 29, 30, 29, 30, 29, 0, 6, 12), 60 => array(6, 30, 29, 30, 29, 30, 30, 29, 30, 29, 30, 29, 30, 29, 7, 1), 61 => array(0, 30, 29, 30, 29, 30, 29, 30, 30, 29, 30, 29, 30, 0, 8, 2), 62 => array(0, 29, 30, 29, 29, 30, 29, 30, 30, 29, 30, 30, 29, 0, 9, 3), 63 => array(4, 30, 29, 30, 29, 29, 30, 29, 30, 29, 30, 30, 30, 29, 10, 4), 64 => array(0, 30, 29, 30, 29, 29, 30, 29, 30, 29, 30, 30, 30, 0, 1, 5), 65 => array(0, 29, 30, 29, 30, 29, 29, 30, 29, 29, 30, 30, 29, 0, 2, 6), 66 => array(3, 30, 30, 30, 29, 30, 29, 29, 30, 29, 29, 30, 30, 29, 3, 7), 67 => array(0, 30, 30, 29, 30, 30, 29, 29, 30, 29, 30, 29, 30, 0, 4, 8), 68 => array(7, 29, 30, 29, 30, 30, 29, 30, 29, 30, 29, 30, 29, 30, 5, 9), 69 => array(0, 29, 30, 29, 30, 29, 30, 30, 29, 30, 29, 30, 29, 0, 6, 10), 70 => array(0, 30, 29, 29, 30, 29, 30, 30, 29, 30, 30, 29, 30, 0, 7, 11), 71 => array(5, 29, 30, 29, 29, 30, 29, 30, 29, 30, 30, 30, 29, 30, 8, 12), 72 => array(0, 29, 30, 29, 29, 30, 29, 30, 29, 30, 30, 29, 30, 0, 9, 1), 73 => array(0, 30, 29, 30, 29, 29, 30, 29, 29, 30, 30, 29, 30, 0, 10, 2), 74 => array(4, 30, 30, 29, 30, 29, 29, 30, 29, 29, 30, 30, 29, 30, 1, 3), 75 => array(0, 30, 30, 29, 30, 29, 29, 30, 29, 29, 30, 29, 30, 0, 2, 4), 76 => array(8, 30, 30, 29, 30, 29, 30, 29, 30, 29, 29, 30, 29, 30, 3, 5), 77 => array(0, 30, 29, 30, 30, 29, 30, 29, 30, 29, 30, 29, 29, 0, 4, 6), 78 => array(0, 30, 29, 30, 30, 29, 30, 30, 29, 30, 29, 30, 29, 0, 5, 7), 79 => array(6, 30, 29, 29, 30, 29, 30, 30, 29, 30, 30, 29, 30, 29, 6, 8), 80 => array(0, 30, 29, 29, 30, 29, 30, 29, 30, 30, 29, 30, 30, 0, 7, 9), 81 => array(0, 29, 30, 29, 29, 30, 29, 29, 30, 30, 29, 30, 30, 0, 8, 10), 82 => array(4, 30, 29, 30, 29, 29, 30, 29, 29, 30, 29, 30, 30, 30, 9, 11), 83 => array(0, 30, 29, 30, 29, 29, 30, 29, 29, 30, 29, 30, 30, 0, 10, 12), 84 => array(10, 30, 29, 30, 30, 29, 29, 30, 29, 29, 30, 29, 30, 30, 1, 1), 85 => array(0, 29, 30, 30, 29, 30, 29, 30, 29, 29, 30, 29, 30, 0, 2, 2), 86 => array(0, 29, 30, 30, 29, 30, 30, 29, 30, 29, 30, 29, 29, 0, 3, 3), 87 => array(6, 30, 29, 30, 29, 30, 30, 29, 30, 30, 29, 30, 29, 29, 4, 4), 88 => array(0, 30, 29, 30, 29, 30, 29, 30, 30, 29, 30, 30, 29, 0, 5, 5), 89 => array(0, 30, 29, 29, 30, 29, 29, 30, 30, 29, 30, 30, 30, 0, 6, 6), 90 => array(5, 29, 30, 29, 29, 30, 29, 29, 30, 29, 30, 30, 30, 30, 7, 7), 91 => array(0, 29, 30, 29, 29, 30, 29, 29, 30, 29, 30, 30, 30, 0, 8, 8), 92 => array(0, 29, 30, 30, 29, 29, 30, 29, 29, 30, 29, 30, 30, 0, 9, 9), 93 => array(3, 29, 30, 30, 29, 30, 29, 30, 29, 29, 30, 29, 30, 29, 10, 10), 94 => array(0, 30, 30, 30, 29, 30, 29, 30, 29, 29, 30, 29, 30, 0, 1, 11), 95 => array(8, 29, 30, 30, 29, 30, 29, 30, 30, 29, 29, 30, 29, 30, 2, 12), 96 => array(0, 29, 30, 29, 30, 30, 29, 30, 29, 30, 30, 29, 29, 0, 3, 1), 97 => array(0, 30, 29, 30, 29, 30, 29, 30, 30, 29, 30, 30, 29, 0, 4, 2), 98 => array(5, 30, 29, 29, 30, 29, 29, 30, 30, 29, 30, 30, 29, 30, 5, 3), 99 => array(0, 30, 29, 29, 30, 29, 29, 30, 29, 30, 30, 30, 29, 0, 6, 4), 100 => array(0, 30, 30, 29, 29, 30, 29, 29, 30, 29, 30, 30, 29, 0, 7, 5), 101 => array(4, 30, 30, 29, 30, 29, 30, 29, 29, 30, 29, 30, 29, 30, 8, 6), 102 => array(0, 30, 30, 29, 30, 29, 30, 29, 29, 30, 29, 30, 29, 0, 9, 7), 103 => array(0, 30, 30, 29, 30, 30, 29, 30, 29, 29, 30, 29, 30, 0, 10, 8), 104 => array(2, 29, 30, 29, 30, 30, 29, 30, 29, 30, 29, 30, 29, 30, 1, 9), 105 => array(0, 29, 30, 29, 30, 29, 30, 30, 29, 30, 29, 30, 29, 0, 2, 10), 106 => array(7, 30, 29, 30, 29, 30, 29, 30, 29, 30, 30, 29, 30, 30, 3, 11), 107 => array(0, 29, 29, 30, 29, 29, 30, 29, 30, 30, 30, 29, 30, 0, 4, 12), 108 => array(0, 30, 29, 29, 30, 29, 29, 30, 29, 30, 30, 29, 30, 0, 5, 1), 109 => array(5, 30, 30, 29, 29, 30, 29, 29, 30, 29, 30, 29, 30, 30, 6, 2), 110 => array(0, 30, 29, 30, 29, 30, 29, 29, 30, 29, 30, 29, 30, 0, 7, 3), 111 => array(0, 30, 29, 30, 30, 29, 30, 29, 29, 30, 29, 30, 29, 0, 8, 4), 112 => array(4, 30, 29, 30, 30, 29, 30, 29, 30, 29, 30, 29, 30, 29, 9, 5), 113 => array(0, 30, 29, 30, 29, 30, 30, 29, 30, 29, 30, 29, 30, 0, 10, 6), 114 => array(9, 29, 30, 29, 30, 29, 30, 29, 30, 30, 29, 30, 29, 30, 1, 7), 115 => array(0, 29, 30, 29, 29, 30, 29, 30, 30, 30, 29, 30, 29, 0, 2, 8), 116 => array(0, 30, 29, 30, 29, 29, 30, 29, 30, 30, 29, 30, 30, 0, 3, 9), 117 => array(6, 29, 30, 29, 30, 29, 29, 30, 29, 30, 29, 30, 30, 30, 4, 10), 118 => array(0, 29, 30, 29, 30, 29, 29, 30, 29, 30, 29, 30, 30, 0, 5, 11), 119 => array(0, 30, 29, 30, 29, 30, 29, 29, 30, 29, 29, 30, 30, 0, 6, 12), 120 => array(4, 29, 30, 30, 30, 29, 30, 29, 29, 30, 29, 30, 29, 30, 7, 1)); //农历天干 $mten = $lnlunarcalendar['tiangan']; //农历地支 $mtwelve = $lnlunarcalendar['dizhi']; //农历月份 $mmonth = $lnlunarcalendar['month']; //农历日 $mday = $lnlunarcalendar['day']; //阳历总天数 至1900年12月21日 $total = 69 * 365 + 17 + 11; //1970年1月1日前的就不算了 if ($year == "" || $month == "" || ($year < 1970 or $year > 2020)) return ''; //超出这个范围不计算 //计算到所求日期阳历的总天数-自1900年12月21日始 //先算年的和 for ($y = 1970; $y < $year; $y++) { $total+=365; if ($y % 4 == 0) $total++; } //再加当年的几个月 $total+=gmdate("z", gmmktime(0, 0, 0, $month, 1, $year)); //用农历的天数累加来判断是否超过阳历的天数 $flag1 = 0; //判断跳出循环的条件 $lcj = 0; while ($lcj <= 120) { $lci = 1; while ($lci <= 13) { $mtotal+=$everymonth[$lcj][$lci]; if ($mtotal >= $total) { $flag1 = 1; break; } $lci++; } if ($flag1 == 1) break; $lcj++; } //由上,得到的 $lci 为当前农历月, $lcj 为当前农历年 //计算所求月份1号的农历日期 $fisrtdaylunar = $everymonth[$lcj][$lci] - ($mtotal - $total); $results['year'] = $mten[$everymonth[$lcj][14]] . $mtwelve[$everymonth[$lcj][15]]; //当前是什么年 $daysthismonth = gmdate("t", gmmktime(0, 0, 0, $month, 1, $year)); //当前月共几天 $op = 1; for ($i = 1; $i <= $daysthismonth; $i++) { $possiblelunarday = $fisrtdaylunar + $op - 1; //理论上叠加后的农历日 if ($possiblelunarday <= $everymonth[$lcj][$lci]) { //在本月的天数范畴内 $results[$i] = $mday[$possiblelunarday]; $op+=1; } else { //不在本月的天数范畴内 $results[$i] = $mday[1]; //退回到1日 $fisrtdaylunar = 1; $op = 2; $curmonthnum = ($everymonth[$lcj][0] != 0) ? 13 : 12; //当年有几个月 if ($lci + 1 > $curmonthnum) { //第13/14个月了,转到下一年 $lci = 1; $lcj = $lcj + 1; //换年头了,把新一年的天干地支也写上 $results['year'].='/' . $mten[$everymonth[$lcj][14]] . $mtwelve[$everymonth[$lcj][15]]; } else { //还在这年里 $lci = $lci + 1; $lcj = $lcj; } } if ($results[$i] == $mday[1]) { //每月的初一应该显示当月是什么月 if ($everymonth[$lcj][0] != 0) { //有闰月的年 $monthss = ($lci > $everymonth[$lcj][0]) ? ($lci - 1) : $lci; //闰月后的月数-1 if ($lci == $everymonth[$lcj][0] + 1) { //这个月正好是闰月 $monthssshow = $mmonth[0] . $mmonth[$monthss]; //前面加个闰字 $runyue = 1; } else { $monthssshow = $mmonth[$monthss]; } } else { $monthss = $lci; $monthssshow = $mmonth[$monthss]; } if ($monthss <= 10 && $runyue != 1) $monthssshow.=$mmonth[13]; //只有1个字的月加上‘月’字 $results[$i] = $monthssshow; } } return $results; } //忘了加上这个: //农历用字 $lnlunarcalendar = array( 'tiangan' => array("未知", "甲", "乙", "丙", "丁", "戊", "己", "庚", "辛", "壬", "癸"), 'dizhi' => array("未知", "子年(鼠)", "丑年(牛)", "寅年(虎)", "卯年(兔)", "辰年(龙)", "巳年(蛇)", "午年(马)", "未年(羊)", "申年(猴)", "酉年(鸡)", "戌年(狗)", "亥年(猪)"), 'month' => array("闰", "正", "二", "三", "四", "五", "六", "七", "八", "九", "十", "十一", "十二", "月"), 'day' => array("未知", "初一", "初二", "初三", "初四", "初五", "初六", "初七", "初八", "初九", "初十", "十一", "十二", "十三", "十四", "十五", "十六", "十七", "十八", "十九", "二十", "廿一", "廿二", "廿三", "廿四", "廿五", "廿六", "廿七", "廿八", "廿九", "三十") ); print_r(lunarcalendar(12, 2010));
2. PHP计算节日、节气、农阳历互换的代码
<?php /* * 农历 节气 节日 * edit: www.jbxue.com */ class Lunar { var $MIN_YEAR = 1891; var $MAX_YEAR = 2100; var $lunarInfo = array( array(0,2,9,21936),array(6,1,30,9656),array(0,2,17,9584),array(0,2,6,21168),array(5,1,26,43344),array(0,2,13,59728), array(0,2,2,27296),array(3,1,22,44368),array(0,2,10,43856),array(8,1,30,19304),array(0,2,19,19168),array(0,2,8,42352), array(5,1,29,21096),array(0,2,16,53856),array(0,2,4,55632),array(4,1,25,27304),array(0,2,13,22176),array(0,2,2,39632), array(2,1,22,19176),array(0,2,10,19168),array(6,1,30,42200),array(0,2,18,42192),array(0,2,6,53840),array(5,1,26,54568), array(0,2,14,46400),array(0,2,3,54944),array(2,1,23,38608),array(0,2,11,38320),array(7,2,1,18872),array(0,2,20,18800), array(0,2,8,42160),array(5,1,28,45656),array(0,2,16,27216),array(0,2,5,27968),array(4,1,24,44456),array(0,2,13,11104), array(0,2,2,38256),array(2,1,23,18808),array(0,2,10,18800),array(6,1,30,25776),array(0,2,17,54432),array(0,2,6,59984), array(5,1,26,27976),array(0,2,14,23248),array(0,2,4,11104),array(3,1,24,37744),array(0,2,11,37600),array(7,1,31,51560), array(0,2,19,51536),array(0,2,8,54432),array(6,1,27,55888),array(0,2,15,46416),array(0,2,5,22176),array(4,1,25,43736), array(0,2,13,9680),array(0,2,2,37584),array(2,1,22,51544),array(0,2,10,43344),array(7,1,29,46248),array(0,2,17,27808), array(0,2,6,46416),array(5,1,27,21928),array(0,2,14,19872),array(0,2,3,42416),array(3,1,24,21176),array(0,2,12,21168), array(8,1,31,43344),array(0,2,18,59728),array(0,2,8,27296),array(6,1,28,44368),array(0,2,15,43856),array(0,2,5,19296), array(4,1,25,42352),array(0,2,13,42352),array(0,2,2,21088),array(3,1,21,59696),array(0,2,9,55632),array(7,1,30,23208), array(0,2,17,22176),array(0,2,6,38608),array(5,1,27,19176),array(0,2,15,19152),array(0,2,3,42192),array(4,1,23,53864), array(0,2,11,53840),array(8,1,31,54568),array(0,2,18,46400),array(0,2,7,46752),array(6,1,28,38608),array(0,2,16,38320), array(0,2,5,18864),array(4,1,25,42168),array(0,2,13,42160),array(10,2,2,45656),array(0,2,20,27216),array(0,2,9,27968), array(6,1,29,44448),array(0,2,17,43872),array(0,2,6,38256),array(5,1,27,18808),array(0,2,15,18800),array(0,2,4,25776), array(3,1,23,27216),array(0,2,10,59984),array(8,1,31,27432),array(0,2,19,23232),array(0,2,7,43872),array(5,1,28,37736), array(0,2,16,37600),array(0,2,5,51552),array(4,1,24,54440),array(0,2,12,54432),array(0,2,1,55888),array(2,1,22,23208), array(0,2,9,22176),array(7,1,29,43736),array(0,2,18,9680),array(0,2,7,37584),array(5,1,26,51544),array(0,2,14,43344), array(0,2,3,46240),array(4,1,23,46416),array(0,2,10,44368),array(9,1,31,21928),array(0,2,19,19360),array(0,2,8,42416), array(6,1,28,21176),array(0,2,16,21168),array(0,2,5,43312),array(4,1,25,29864),array(0,2,12,27296),array(0,2,1,44368), array(2,1,22,19880),array(0,2,10,19296),array(6,1,29,42352),array(0,2,17,42208),array(0,2,6,53856),array(5,1,26,59696), array(0,2,13,54576),array(0,2,3,23200),array(3,1,23,27472),array(0,2,11,38608),array(11,1,31,19176),array(0,2,19,19152), array(0,2,8,42192),array(6,1,28,53848),array(0,2,15,53840),array(0,2,4,54560),array(5,1,24,55968),array(0,2,12,46496), array(0,2,1,22224),array(2,1,22,19160),array(0,2,10,18864),array(7,1,30,42168),array(0,2,17,42160),array(0,2,6,43600), array(5,1,26,46376),array(0,2,14,27936),array(0,2,2,44448),array(3,1,23,21936),array(0,2,11,37744),array(8,2,1,18808), array(0,2,19,18800),array(0,2,8,25776),array(6,1,28,27216),array(0,2,15,59984),array(0,2,4,27424),array(4,1,24,43872), array(0,2,12,43744),array(0,2,2,37600),array(3,1,21,51568),array(0,2,9,51552),array(7,1,29,54440),array(0,2,17,54432), array(0,2,5,55888),array(5,1,26,23208),array(0,2,14,22176),array(0,2,3,42704),array(4,1,23,21224),array(0,2,11,21200), array(8,1,31,43352),array(0,2,19,43344),array(0,2,7,46240),array(6,1,27,46416),array(0,2,15,44368),array(0,2,5,21920), array(4,1,24,42448),array(0,2,12,42416),array(0,2,2,21168),array(3,1,22,43320),array(0,2,9,26928),array(7,1,29,29336), array(0,2,17,27296),array(0,2,6,44368),array(5,1,26,19880),array(0,2,14,19296),array(0,2,3,42352),array(4,1,24,21104), array(0,2,10,53856),array(8,1,30,59696),array(0,2,18,54560),array(0,2,7,55968),array(6,1,27,27472),array(0,2,15,22224), array(0,2,5,19168),array(4,1,25,42216),array(0,2,12,42192),array(0,2,1,53584),array(2,1,21,55592),array(0,2,9,54560) ); /** * 将阳历转换为阴历 * @param year 公历-年 * @param month 公历-月 * @param date 公历-日 */ function convertSolarToLunar($year,$month,$date) { //debugger; $yearData = $this->lunarInfo[$year-$this->MIN_YEAR]; if($year==$this->MIN_YEAR&&$month<=2&&$date<=9) return array(1891,'正月','初一','辛卯',1,1,'兔'); return $this->getLunarByBetween($year,$this->getDaysBetweenSolar($year,$month,$date,$yearData[1],$yearData[2])); } function convertSolarMonthToLunar($year,$month) { $yearData = $this->lunarInfo[$year-$this->MIN_YEAR]; if($year==$this->MIN_YEAR&&$month<=2&&$date<=9) return array(1891,'正月','初一','辛卯',1,1,'兔'); $month_days_ary = array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31); $dd = $month_days_ary[$month]; if($this->isLeapYear($year) && $month == 2) $dd++; $lunar_ary = array(); for ($i = 1; $i < $dd; $i++) { $array = $this->getLunarByBetween($year,$this->getDaysBetweenSolar($year, $month, $i, $yearData[1], $yearData[2])); $array[] = $year . '-' . $month . '-' . $i; $lunar_ary[$i] = $array; } return $lunar_ary; } /** * 将阴历转换为阳历 * @param year 阴历-年 * @param month 阴历-月,闰月处理:例如如果当年闰五月,那么第二个五月就传六月,相当于阴历有13个月,只是有的时候第13个月的天数为0 * @param date 阴历-日 */ function convertLunarToSolar($year,$month,$date) { $yearData = $this->lunarInfo[$year-$this->MIN_YEAR]; $between = $this->getDaysBetweenLunar($year,$month,$date); $res = mktime(0,0,0,$yearData[1],$yearData[2],$year); $res = date('Y-m-d', $res+$between*24*60*60); $day = explode('-', $res); $year = $day[0]; $month= $day[1]; $day = $day[2]; return array($year, $month, $day); } /** * 判断是否是闰年 * @param year */ function isLeapYear($year) { return (($year%4==0 && $year%100 !=0) || ($year%400==0)); } /** * 获取干支纪年 * @param year */ function getLunarYearName($year) { $sky = array('庚','辛','壬','癸','甲','乙','丙','丁','戊','己'); $earth = array('申','酉','戌','亥','子','丑','寅','卯','辰','巳','午','未'); $year = $year.''; return $sky[$year{3}].$earth[$year%12]; } /** * 根据阴历年获取生肖 * @param year 阴历年 */ function getYearZodiac($year) { $zodiac = array('猴','鸡','狗','猪','鼠','牛','虎','兔','龙','蛇','马','羊'); return $zodiac[$year%12]; } /** * 获取阳历月份的天数 * @param year 阳历-年 * @param month 阳历-月 */ function getSolarMonthDays($year,$month) { $monthHash = array('1'=>31,'2'=>$this->isLeapYear($year)?29:28,'3'=>31,'4'=>30,'5'=>31,'6'=>30,'7'=>31,'8'=>31,'9'=>30,'10'=>31,'11'=>30,'12'=>31); return $monthHash["$month"]; } /** * 获取阴历月份的天数 * @param year 阴历-年 * @param month 阴历-月,从一月开始 */ function getLunarMonthDays($year,$month) { $monthData = $this->getLunarMonths($year); return $monthData[$month-1]; } /** * 获取阴历每月的天数的数组 * @param year */ function getLunarMonths($year) { $yearData = $this->lunarInfo[$year - $this->MIN_YEAR]; $leapMonth = $yearData[0]; $bit = decbin($yearData[3]); for ($i = 0; $i < strlen($bit);$i ++) $bitArray[$i] = substr($bit, $i, 1); for($k=0,$klen=16-count($bitArray);$k<$klen;$k++) array_unshift($bitArray, '0'); $bitArray = array_slice($bitArray,0,($leapMonth==0?12:13)); for($i=0; $i<count($bitArray); $i++) $bitArray[$i] = $bitArray[$i] + 29; return $bitArray; } /** * 获取农历每年的天数 * @param year 农历年份 */ function getLunarYearDays($year) { $yearData = $this->lunarInfo[$year-$this->MIN_YEAR]; $monthArray = $this->getLunarYearMonths($year); $len = count($monthArray); return ($monthArray[$len-1]==0?$monthArray[$len-2]:$monthArray[$len-1]); } function getLunarYearMonths($year) { //debugger; $monthData = $this->getLunarMonths($year); $res=array(); $temp=0; $yearData = $this->lunarInfo[$year-$this->MIN_YEAR]; $len = ($yearData[0]==0?12:13); for($i=0;$i<$len;$i++) { $temp=0; for($j=0;$j<=$i;$j++) $temp+=$monthData[$j]; array_push($res, $temp); } return $res; } /** * 获取闰月 * @param year 阴历年份 */ function getLeapMonth($year) { $yearData = $this->lunarInfo[$year-$this->MIN_YEAR]; return $yearData[0]; } /** * 计算阴历日期与正月初一相隔的天数 * @param year * @param month * @param date */ function getDaysBetweenLunar($year,$month,$date) { $yearMonth = $this->getLunarMonths($year); $res=0; for($i=1;$i<$month;$i++) $res +=$yearMonth[$i-1]; $res+=$date-1; return $res; } /** * 计算2个阳历日期之间的天数 * @param year 阳历年 * @param cmonth * @param cdate * @param dmonth 阴历正月对应的阳历月份 * @param ddate 阴历初一对应的阳历天数 */ function getDaysBetweenSolar($year,$cmonth,$cdate,$dmonth,$ddate) { $a = mktime(0,0,0,$cmonth,$cdate,$year); $b = mktime(0,0,0,$dmonth,$ddate,$year); return ceil(($a-$b)/24/3600); } /** * 根据距离正月初一的天数计算阴历日期 * @param year 阳历年 * @param between 天数 */ function getLunarByBetween($year,$between) { //debugger; $lunarArray = array(); $yearMonth=array(); $t=0; $e=0; $leapMonth=0; $m=''; if($between==0) { array_push($lunarArray, $year,'正月','初一'); $t = 1; $e = 1; } else { $year = $between>0? $year : ($year-1); $yearMonth = $this->getLunarYearMonths($year); $leapMonth = $this->getLeapMonth($year); $between = $between>0?$between : ($this->getLunarYearDays($year)+$between); for($i=0;$i<13;$i++) { if($between==$yearMonth[$i]) { $t=$i+2; $e=1; break; }else if($between<$yearMonth[$i]) { $t=$i+1; $e=$between-(empty($yearMonth[$i-1])?0:$yearMonth[$i-1])+1; break; } } $m = ($leapMonth!=0&&$t==$leapMonth+1)?('闰'.$this->getCapitalNum($t- 1,true)):$this->getCapitalNum(($leapMonth!=0&&$leapMonth+1<$t?($t-1):$t),true); array_push($lunarArray,$year,$m,$this->getCapitalNum($e,false)); } array_push($lunarArray,$this->getLunarYearName($year));// 天干地支 array_push($lunarArray,$t,$e); array_push($lunarArray,$this->getYearZodiac($year));// 12生肖 array_push($lunarArray,$leapMonth);// 闰几月 return $lunarArray; } /** * 获取数字的阴历叫法 * @param num 数字 * @param isMonth 是否是月份的数字 */ function getCapitalNum($num,$isMonth) { $isMonth = $isMonth || false; $dateHash=array('0'=>'','1'=>'一','2'=>'二','3'=>'三','4'=>'四','5'=>'五','6'=>'六','7'=>'七','8'=>'八','9'=>'九','10'=>'十 '); $monthHash=array('0'=>'','1'=>'正月','2'=>'二月','3'=>'三月','4'=>'四月','5'=>'五月','6'=>'六月','7'=>'七月','8'=>'八月','9'=>'九月','10'=>'十月','11'=>'冬月','12'=>'腊月'); $res=''; if($isMonth) $res = $monthHash[$num]; else { if($num<=10) $res = '初'.$dateHash[$num]; else if($num>10&&$num<20) $res = '十'.$dateHash[$num-10]; else if($num==20) $res = "二十"; else if($num>20&&$num<30) $res = "廿".$dateHash[$num-20]; else if($num==30) $res = "三十"; } return $res; } /* * 节气通用算法 */ function getJieQi($_year,$month,$day) { $year = substr($_year,-2)+0; $coefficient = array( array(5.4055,2019,-1),//小寒 array(20.12,2082,1),//大寒 array(3.87),//立春 array(18.74,2026,-1),//雨水 array(5.63),//惊蛰 array(20.646,2084,1),//春分 array(4.81),//清明 array(20.1),//谷雨 array(5.52,1911,1),//立夏 array(21.04,2008,1),//小满 array(5.678,1902,1),//芒种 array(21.37,1928,1),//夏至 array(7.108,2016,1),//小暑 array(22.83,1922,1),//大暑 array(7.5,2002,1),//立秋 array(23.13),//处暑 array(7.646,1927,1),//白露 array(23.042,1942,1),//秋分 array(8.318),//寒露 array(23.438,2089,1),//霜降 array(7.438,2089,1),//立冬 array(22.36,1978,1),//小雪 array(7.18,1954,1),//大雪 array(21.94,2021,-1)//冬至 ); $term_name = array( "小寒","大寒","立春","雨水","惊蛰","春分","清明","谷雨", "立夏","小满","芒种","夏至","小暑","大暑","立秋","处暑", "白露","秋分","寒露","霜降","立冬","小雪","大雪","冬至"); $idx1 = ($month-1)*2; $_leap_value = floor(($year-1)/4); $day1 = floor($year*0.2422+$coefficient[$idx1][0])-$_leap_value; if(isset($coefficient[$idx1][1])&&$coefficient[$idx1][1]==$_year) $day1 += $coefficient[$idx1][2]; $day2 = floor($year*0.2422+$coefficient[$idx1+1][0])-$_leap_value; if(isset($coefficient[$idx1+1][1])&&$coefficient[$idx1+1][1]==$_year) $day1 += $coefficient[$idx1+1][2]; //echo __FILE__.'->'.__LINE__.' $day1='.$day1,',$day2='.$day2.'<br/>'.chr(10); if($day==$day1) return $term_name[$idx1]; if($day==$day2) return $term_name[$idx1+1]; return ''; } /* * 获取节日:特殊的节日只能修改此函数来计算 */ function getFestival($today, $nl_info = false,$config = 1) { if($config == 1) { $arr_lunar=array('01-01'=>'春节','01-15'=>'元宵节','02-02'=>'二月二','05-05'=>'端午节','07-07'=>'七夕节','08-15'=>'中秋节','09-09'=>'重阳节','12-08'=>'腊八节','12-23'=>'小年'); $arr_solar=array('01-01'=>'元旦','02-14'=>'情人节','03-12'=>'植树节','04-01'=>'愚人节','05-01'=>'劳动节','06-01'=>'儿童节','10-01'=>'国庆节','10-31'=>'万圣节','12-24'=>'平安夜','12-25'=>'圣诞节'); }//需要不同节日的,用不同的$config,然后配置$arr_lunar和$arr_solar $festivals = array(); list($y,$m,$d) = explode('-',$today); if(!$nl_info) $nl_info = $this->convertSolarToLunar($y,intval($m),intval($d)); if($nl_info[7]>0&&$nl_info[7]<$nl_info[4]) $nl_info[4]-=1; $md_lunar = substr('0'.$nl_info[4],-2).'-'.substr('0'.$nl_info[5],-2); $md_solar=substr_replace($today,'',0,5); isset($arr_lunar[$md_lunar])?array_push($festivals, $arr_lunar[$md_lunar]):''; isset($arr_solar[$md_solar])?array_push($festivals, $arr_solar[$md_solar]):''; $glweek = date("w",strtotime($today)); //0-6 if($m==5&&($d>7)&&($d<15)&&($glweek==0))array_push($festivals, "母亲节"); if($m==6&&($d>14)&&($d<22)&&($glweek==0))array_push($festivals,"父亲节"); $jieqi = $this->getJieQi($y,$m,$d); if($jieqi)array_push($festivals,$jieqi); return implode('/',$festivals); } } /* header("Content-Type:text/html;charset=utf-8"); $lunar=new Lunar();// $month=$lunar->convertSolarToLunar(2014,01,13);//将阳历转换为阴历 echo '<pre>'; print_r($month); */
来源:http://www.jbxue.com/article/12689.html
相关推荐
例如,可以利用太阳黄经和地球公转周期(回归年)的关系,结合农历日期计算节气。 4. **编程实现**:创建一个函数,输入当前日期,输出对应的节气。这个函数需要包含计算节气的逻辑,比如检查日期是否位于两个节气...
`Calendar`是Java提供的一个抽象类,用于表示特定瞬间的时间值,可以被用来执行与日历和时间相关的操作,如获取当前时间、设置时间、计算日期差等。在Java中,`Calendar`类的一个具体子类`GregorianCalendar`通常被...
在C#编程中,计算当月的工作日天数是一项常见的任务,尤其在处理日期相关的业务逻辑时。这个任务可以通过各种方法实现,对于初学者来说,理解并编写这样的代码是提升编程技能的重要步骤。下面我们将详细探讨如何使用...
总结一下,JavaScript获取当月第一天和最后一天的关键在于理解Date对象的属性和方法,并通过适当的计算调整日期。通过`setDate(1)`可以得到当月第一天,通过计算下个月第一天并减去一天可得到当月最后一天。这些知识...
### JavaScript 获取当月天数 #### 知识点解析 在给定的代码示例中,我们可以看到一段 Java 代码,而不是 JavaScript。这段 Java 代码主要用于计算并打印出指定年份和月份的天数。然而,题目要求的是使用 ...
在PHP中,计算请假时长是一项常见的任务,特别是在开发考勤系统时。这个任务涉及到对时间戳的理解,以及如何在日期之间正确地计算差值。下面我们将深入探讨这些知识点。 首先,时间戳是自1970年1月1日(UTC/GMT的...
标题“javascript获取当月第一天和最后一天”涉及的是JavaScript编程中的日期处理技术,特别是如何获取当前月份的第一天和最后一天的日期。JavaScript的Date对象是处理日期和时间的主要工具,但默认方法并不直接提供...
文章分享了一个非常实用的PHP函数,这个函数能够计算出当月的最后一天是几号。我们可以将其用在任何需要计算月底日期的场景中,比如自动设置账单支付日期为下个月的第一天,或者为某个特定月份生成的日历找到正确的...
在PHP编程中,有时我们需要获取特定年月的天数,以及该月的每一天的日期,以便进行各种日期相关的计算或处理。以下是一个简单的PHP函数,它根据输入的日期(年月格式)返回当月的天数及日期数组。 首先,我们来详细...
### JavaScript 获取年月日及当月天数 在JavaScript中,日期对象提供了丰富的API来处理时间与日期。本文将详细介绍如何使用JavaScript获取当前日期的年、月、日以及当月的总天数。 #### 一、JavaScript日期对象...
公历、农历互换组件 TransDate (DELPHI 7 转换日期1900 -- 2056)TransDate组件可将公历转换成农历,公历包括星座,星期。农历数据包含节气,干支历,生肖。也可将农历转换成公历,转换过程自动完成。即输入公历日期...
在日常生活中,农历(阴历)与公历(阳历)之间的转换对于节日庆祝、传统活动等有着重要的意义。例如,春节、中秋节等中国传统节日都是根据农历来确定日期的。因此,开发一个能够准确进行日期转换的功能是非常必要的...
输入公历日期显示月历 月历上显示所输入日期的星期、农历公历日期、农历的天干地支和生肖、当天是否是节气,如果是,则显示,不是,则显示距离它最近的节气的间隔日期、列出当月的二十四节气; 2.输入农历日期,显示...
获取当月中的最大一天代码
万年历通常是指一种显示特定日期范围内日程和日期信息的工具,这里我们关注的是如何通过纯代码实现,不依赖任何图形用户界面(GUI)。 首先,我们需要了解Java中的日期和时间API。在Java 8及更高版本中,`java.time...
在编程领域,获取特定年月的天数是一项常见的任务,特别是在...如果你有一个名为"根据年月得到当月的天数及日期.txt"的文本文件,可能包含了这样的代码实现或更多详细说明,你可以阅读文件内容以获取更具体的实现细节。
可精确计算4000年节气的AS3代码。 可返回当日前时间(精确到分秒),当日时间后的节与气(用于命理等程序开发),当月准确的两个节与气的时间
根据提供的文件信息,我们可以总结出以下关于Java中计算某个月工作日的相关知识点: ### Java中计算某个月的工作日总数及具体工作日 #### 概述 在Java中,要计算一个月内有多少个工作日(通常指周一至周五),以及...
在获取月份第一天的代码中,`mktime(0,0,0,date('m',strtotime($date))+1,1)`这部分是关键,其中`strtotime($date)`将当前日期转换为Unix时间戳,`date('m',strtotime($date))+1`表示将当前月份增加1,这样`mktime(0...