网上转换方法很多例子错误。

测试例子1:输入公历 2010年2月1号测试,对比百度万年历 农历应该为己丑年(2009)腊月(12月)十八。

测试例子2:输入农历1990.11.初十,丑时,公历应该为1990年12月26日 2时。

<?php

function nongli($riqi)
{
//优化修改 20160807 FXL
$nian=date('Y',strtotime($riqi));
$yue=date('m',strtotime($riqi));
$ri=date('d',strtotime($riqi)); #源码部分原作者:沈潋(S&S Lab)
#农历每月的天数
$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=array("null","甲","乙","丙","丁","戊","己","庚","辛","壬","癸");
#农历地支
$mtwelve=array("null","子(鼠)","丑(牛)","寅(虎)","卯(兔)","辰(龙)",
"巳(蛇)","午(马)","未(羊)","申(猴)","酉(鸡)","戌(狗)","亥(猪)");
#农历月份
$mmonth=array("闰","正","二","三","四","五","六",
"七","八","九","十","十一","十二","月");
#农历日
$mday=array("null","初一","初二","初三","初四","初五","初六","初七","初八","初九","初十",
"十一","十二","十三","十四","十五","十六","十七","十八","十九","二十",
"廿一","廿二","廿三","廿四","廿五","廿六","廿七","廿八","廿九","三十");
##############################
#星期
$weekday = array("星期日","星期一","星期二","星期三","星期四","星期五","星期六");
#阳历总天数 至1900年12月21日
$total=11;
#阴历总天数
$mtotal=0;
##############################
#获得当日日期
//$today=getdate(); //获取今天的日期
if($nian<1901 || $nian>2020) die("年份出错!");
//$cur_wday=$today["wday"]; //星期中第几天的数字表示
for($y=1901;$y<$nian;$y++) { //计算到所求日期阳历的总天数-自1900年12月21日始,先算年的和
$total+=365;
if ($y%4==0) $total++;
}
switch($yue) { //再加当年的几个月
case 12:
$total+=30;
case 11:
$total+=31;
case 10:
$total+=30;
case 9:
$total+=31;
case 8:
$total+=31;
case 7:
$total+=30;
case 6:
$total+=31;
case 5:
$total+=30;
case 4:
$total+=31;
case 3:
$total+=28;
case 2:
$total+=31;
}
if($nian%4 == 0 && $yue>2) $total++; //如果当年是闰年还要加一天
$total=$total+$ri-1; //加当月的天数
$flag1=0; //判断跳出循环的条件
$j=0;
while ($j<=120){ //用农历的天数累加来判断是否超过阳历的天数
$i=1;
while ($i<=13){
$mtotal+=$everymonth[$j][$i];
if ($mtotal>=$total){
$flag1=1;
break;
}
$i++;
}
if ($flag1==1) break;
$j++;
}
if($everymonth[$j][0]<>0 and $everymonth[$j][0]<$i){ //原来错在这里,对闰月没有修补
$mm=$i-1;
}
else{
$mm=$i;
}
if($i==$everymonth[$j][0]+1 and $everymonth[$j][0]<>0) {
$nlmon=$mmonth[0].$mmonth[$mm];#闰月
}
else {
$nlmon=$mmonth[$mm].$mmonth[13];
}
#计算所求月份1号的农历日期
$md=$everymonth[$j][$i]-($mtotal-$total);
if($md > $everymonth[$j][$i])
$md-=$everymonth[$j][$i];
$nlday=$mday[$md]; //$nowday=date("Y年n月j日 ")."w".$weekday[$cur_wday]." ".$mten[$everymonth[$j][14]].$mtwelve[$everymonth[$j][15]]."年".$nlmon.$nlday;
$nowday=$mten[$everymonth[$j][14]].$mtwelve[$everymonth[$j][15]]."年 ".$nlmon.$nlday;
return $nowday;
}
echo nongli('20100201'); //标准8位

php 阳历转农历优化版的更多相关文章

  1. 通用PE工具箱 4.0精简优化版

    通用PE工具箱 4.0精简优化版 经用过不少 WinPE 系统,都不是很满意,普遍存在篡改主页.添加广告链接至收藏夹.未经允许安装推广软件等流氓行为,还集成了诸多不常用的工具,令人头疼不已.那么今天给 ...

  2. GHOST WIN7系统64位经典优化版 V2016年

    来自系统妈:http://www.xitongma.com 深度技术GHOST win7系统32,64位经典优化版 V2016年3月 系统概述 深度技术ghost win7系统64位经典优化版适用于笔 ...

  3. Chrome 扩展程序 CrxMouse 优化版 v3.0.1

    说明 CrxMouse 原版更新至 v2.7.8,跟进升级优化版至 v3.0.1. 改动说明: 1. 去除可能存在的后台数据上传隐患: 2. 解决鼠标右键拖动时的轨迹漂移问题. 3. 加入部分默认设置 ...

  4. POJ-2533最长上升子序列(DP+二分)(优化版)

    Longest Ordered Subsequence Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 41944   Acc ...

  5. 搜狗拼音输入法 V9.1.0.2589 最新去广告精简优化版

    搜狗拼音输入法9.0 正式版例行发布,最新版字母代号b,详细版本号为v9.1.0.2589:搜狗拼音输入法是电脑装机必备软件,版本有传统版和智慧版之分,其打字超准.词库超大.速度飞快.外观漂亮,因此使 ...

  6. 【转】Appium 优化版

    Appium 开源分享优化版 之前分享过PageObject+Python+Appium 本版本是对上次版本较大改版,主要解决了: 失败重连一次(默认一次)可配置多次 基于appium1.7.1 ui ...

  7. 将多个图片合并到一个TIF文件里(非 GDAL) 优化版

    不知道为什么,网上对TIF的操作的资料少得可怜,包括CodeProject上都没有找到多少,在网上大多用GDAL,但这个东西,对只想做个合并图片的功能来说,实在是牛刀杀鸡,(9个DLL要带全,相当的恐 ...

  8. m3m4加载器的优化版m3m4-v1.1

    m3m4加载器的优化版m3m4-v1.1 /* //1.以$开头的模块名,表示服务,服务只会执行一次,它可以有自己的方法.比如cookie就是一个服务 //例如:console.log("r ...

  9. Linux-shell实现阳历转农历(序)

    好些天没有登陆邮箱,前几天上班打开一看垃圾箱中有一封邮件让我好激动,还是国外友人的英文邮件.^_^大概内容是我早些时候写的一个阳历转农历的shell小程序,他在用的时候发现了bug,但是这个bug我在 ...

随机推荐

  1. Python小数据池,代码块

    今日内容一些小的干货 一. id is == 二. 代码块 三. 小数据池 四. 总结 python小数据池,代码块的最详细.深入剖析   一. id is == 二. 代码块 三. 小数据池 四. ...

  2. 自动出借-python+selenium

    自动出借 import time from selenium import webdriver # import os #B username = " # 请替换成你的用户名 passwor ...

  3. [LeetCode] 405. Convert a Number to Hexadecimal_Easy tag: Bit Manipulation

    Given an integer, write an algorithm to convert it to hexadecimal. For negative integer, two’s compl ...

  4. js模拟队列----小优先队列

    队列:先进先出,后进后出 var Queue = (function(){ var item = new WeakMap(); class Queue{ constructor(){ item.set ...

  5. 配置完centos 6以后,大概需要安装的软件(主要是yum)

    根据实践,把我的经验说一下,以后我自己也可以按照这个快速安装软件. 1. 配置源.百度网盘的tools/download/linux已经放了几个挺重要的 东西了. yum -y install epe ...

  6. iOS UI基础-10.0 QQ聊天布局之键盘及文本使用

    要实现的效果:   这里只说用到的几个知识点 1.图片包含文字 在设置文字的Frame的时候,使用背景(按钮)的尺寸,文字使用了内边距 背景图片,使用拉伸 /** * 返回一张可以随意拉伸不变形的图片 ...

  7. 强势龙头股的 VOLM5/35/135 走势,可作为逃顶参考

    强势龙头股的 VOLM5/35/135 走势,可作为逃顶参考

  8. can not create symbolic link HDFS解压自动配置lib报错。

    如题,使用FusionInsight解压生成样例代码的时候报错,找不到解释.只猜测是权限问题.然后并没有仔细静心思考,心里杂念很多,很浮躁. 解决方法是“以管理员身份运行“. 想想高中:面对问题,不能 ...

  9. Sublime text3 经常出现 “ There are no packages avaliable for installation” 解决方法

    对应这个问题,一开始在度娘上找到很多答案,包括将json文件放在本地然后通过 package setting 更改的,发现其实不好使,原因未知. 后来测试了在hosts文件添加sublime text ...

  10. LeetCode38.报数

    报数序列是一个整数序列,按照其中的整数的顺序进行报数,得到下一个数.其前五项如下: 1. 1 2. 11 3. 21 4. 1211 5. 111221 1 被读作  "one 1" ...