PHP公历农历转换(阴历阳历转换)阴历和阳历转换
1
<?php2
/**3
* 公农历转换(1912 - 2012) 4
* 5
* Usage:6
* // 公历1983-10-5转农历7
* $lunar = new Lunar();8
* $date = $lunar->getLar('1983-10-5',0);9
* echo date("Y-m-d", $date);10
* // 农历1983-8-29转公历11
* $date = $lunar->getLar('1983-8-29',1);12
* echo date("Y-m-d", $date);13
*14
* @param string 日期15
* @param int 日期历法16
* - 0 公历17
* 1 农历18
*19
* @return timestamp20

21
22
这是一个国历与农历互相转的Unit.23

24
其中年份皆用民国年份, 请自行转换 (西元年-1911 = 民国年).25
***************************************************************************26
*国农历对映表之说明 : *27
***************************************************************************28
* 前二数字 = 闰月月份, 如果为 13 则没有闰月 *29
* 第叁至第六数字 = 12 个月之大小月之2进位码->10进位 *30
* 例如: *31
* 101010101010 = 2730 *32
* 1 : 代表大月(30天) 0 : 代表小月(29天) ==> 1月大2月小3月大
.. *33
* 第七位数字为闰月天数 *34
* 0 : 没有闰月之天数 *35
* 1 : 闰月为小月(29天) *36
* 2 : 闰月为大月(30天) *37
* 最後2位数字代表阳历之1月1日与阴历之1月1日相差天数 *38
***************************************************************************39
这对映表只有民国一年至民国一百年, 如不敷您的使用请按照上述之方式自行增加.40

41
这个程式没有判断您所输入之年,月,日是否正确, 请自行判断.42

43
如果转换出来之农历的月份是闰月则传给您的值是***负数***44
如果农历要转换国历如果是闰月请输入***负数***45

46
此版本为FreeWare Version : 0.147
您可以自行修改, 但最好可以将修改过之程式Mail一份给我.48
如果您要用於商业用途, 请mail给我告知您的用途及原因.49
50
*/51

52
class Lunar {53
var $LMDay = array();54
var $InterMonth = 0;55
var $InterMonthDays = 0;56
var $SLRangeDay = 0;57

58
var $SMDay = array(1 => 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);59
var $LongLife = array( 1 =>60
'132637048', '133365036', '053365225', '132900044', '131386034', '022778122', //661
'132395041', '071175231', '131175050', '132635038', '052891127', '131701046', //1262
'131748035', '042741223', '130694043', '132391032', '021327122', '131175040', //1863
'061623129', '133402047', '133402036', '051769125', '131453044', '130694034', //2464
'032158223', '132350041', '073213230', '133221049', '133402038', '063466226', //3065
'132901045', '131130035', '042651224', '130605043', '132349032', '023371121', //3666
'132709040', '072901128', '131738047', '132901036', '051333226', '131210044', //4267
'132651033', '031111223', '131323042', '082714130', '133733048', '131706038', //4868
'062794127', '132741045', '131206035', '042734124', '132647043', '131318032', //5469
'033878120', '133477039', '071461129', '131386047', '132413036', '051245126', //6070
'131197045', '132637033', '043405122', '133365041', '083413130', '132900048', //6671
'132922037', '062394227', '132395046', '131179035', '042711124', '132635043', //7272
'102855132', '131701050', '131748039', '062804128', '132742047', '132359036', //7873
'051199126', '131175045', '131611034', '031866122', '133749040', '081717130', //8474
'131452049', '132742037', '052413127', '132350046', '133222035', '043477123', //9075
'133402042', '133493031', '021877121', '131386039', '072747128', '130605048', //9676
'132349037', '053243125', '132709044', '132890033');77
78
function getLar($date, $isLunar = 1){79
list($year, $month, $day) = split("-", $date);80
if($isLunar == 1)81
return $this->Lunar2Solar($year, $month, $day);82
else83
return $this->Solar2Lunar($year, $month, $day);84
}85
86
function IsLeapYear($AYear){87
return ($AYear % 4 == 0) and (($AYear % 100 <> 0) or ($AYear % 400 == 0));88
}89
90
function CovertLunarMonth($magicno){91
$m = $magicno;92
for ($i = 12; $i >= 1; $i--){93
$size = $m % 2;94
if ($size == 0)95
$this->LMDay[$i] = 29;96
else97
$this->LMDay[$i] = 30;98
$m = floor($m / 2);99
}100
}101
102
function ProcessMagicStr($yy){103
$yy = $yy - 1911;104
$magicstr = $this->LongLife[$yy];105
$this->InterMonth = substr($magicstr, 0, 2);106
$LunarMonth = substr($magicstr, 2, 4);107
$this->CovertLunarMonth($LunarMonth);108
$dsize = substr($magicstr, 6, 1);109
switch ($dsize) {110
case 0 : 111
$this->InterMonthDays = 0;112
break;113
case 1 : 114
$this->InterMonthDays = 29;115
break;116
case 2 : 117
$this->InterMonthDays = 30;118
break;119
}120
$this->SLRangeDay = substr($magicstr, 7, 2);121
}122
123
function DaysPerLunarMonth($LYear, $LMonth){124
$this->;ProcessMagicStr($LYear);125
if ($LMonth < 0)126
return $this->InterMonthDays;127
else128
return $this->LMDay[$LMonth];129
}130
131
function Solar2Lunar($SYear, $SMonth, $SDay){132
if( !(1912 <= $SYear && $SYear <= 2012) ){133
return false;134
}135
$day = 0;136
if ($this->isLeapYear($SYear))137
$this->SMDay[2] = 29;138
$this->;ProcessMagicStr($SYear);139
if ($SMonth == 1)140
$day = $SDay;141
else {142
for($i = 1; $i <= $SMonth-1; $i++)143
$day = $day + $this->SMDay[$i];144
$day = $day + $SDay;145
}146
if ($day <= $this->SLRangeDay) {147
$day = $day - $this->SLRangeDay;148
$this->processmagicstr($SYear-1);149
for ($i = 12; $i >= 1; $i--){150
$day = $day + $this->LMDay[$i];151
if ($day > 0)152
break;153
}154
$LYear = $SYear - 1;155
$LMonth = $i;156
$LDay = $day;157
} else {158
$day = $day - $this->SLRangeDay;159
for($i = 1; $i <= $this->InterMonth-1; $i++){160
$day = $day - $this->LMDay[$i];161
if ($day <= 0)162
break;163
}164
if ($day <= 0) {165
$LYear = $SYear;166
$LMonth = $i;167
$LDay = $day + $this->LMDay[$i];168
} else {169
$day = $day - $this->LMDay[$this->InterMonth];170
if ($day <= 0) {171
$LYear = $SYear;172
$LMonth = $this->InterMonth;173
$LDay = $day + $this->LMDay[$this->InterMonth];174
} else {175
$this->LMDay[$this->InterMonth] = $this->InterMonthDays;176
for ($i = $this->InterMonth; $i <= 12; $i++){177
$day = $day - $this->LMDay[$i];178
if ($day <= 0)179
break;180
}181
if ($i == $this->InterMonth)182
$LMonth = 0 - $this->InterMonth;183
else184
$LMonth = $i;185
$LYear = $SYear;186
$LDay = $day + $this->LMDay[$i];187
}188
}189
}190
return mktime(0, 0, 0, $LMonth, $LDay, $LYear);191
}192
193
function Lunar2Solar($LYear, $LMonth, $LDay){194
if( !(1912 <= $LYear && $LYear <= 2012) ){195
return false;196
}197
$day = 0;198
$SYear = $LYear;199
if ($this->isLeapYear($SYear))200
$this->SMDay[2] = 29;201
$this->processmagicstr($SYear);202
if ($LMonth < 0)203
$day = $this->LMDay[$this->InterMonth];204
if ($LMonth <> 1)205
for ($i = 1; $i <= $LMonth-1; $i++)206
$day = $day + $this->LMDay[$i];207
$day = $day + $LDay + $this->SLRangeDay;208
if (($this->InterMonth <> 13) and ($this->InterMonth < $LMonth))209
$day = $day + $this->InterMonthDays;210
for ($i = 1; $i <= 12; $i++){211
$day = $day - $this->SMDay[$i];212
if ($day <= 0)213
break;214
}215
if ($day > 0) {216
$SYear = $SYear + 1;217
if ($this->isLeapYear($SYear))218
$this->SMDay[2] = 29;219
for ($i = 1; $i <= 12; $i++){220
$day = $day - $this->SMDay[$i];221
if ($day <= 0)222
break;223
}224
}225
$day = $day + $this->SMDay[$i];226
$SMonth = $i;227
$SDay = $day;228
return mktime(0, 0, 0, $SMonth, $SDay, $SYear);229
}230
}231
?>232

233

234

可以歇歇了!!
浙公网安备 33010602011771号