首页  >  文章  >  后端开发  >  PHP实现的汉字拼音转换和公历农历转换类及使用示例_PHP

PHP实现的汉字拼音转换和公历农历转换类及使用示例_PHP

WBOY
WBOY原创
2016-06-01 11:51:461052浏览

本文整理了PHP汉字拼音转换和公历农历转换两个功能类文件,非常实用。比如我们查找通讯录可以通过联系人姓名的拼音首字母来查询,可以通过首字母来导航大数据量,可以通过转换拼音来做网站优化等。公农历转化一般用在日历日程安排的项目中,方便农历的节日提醒等等。

1、PHP汉字转拼音

Pinyin.class.php类文件可以将大多数汉字转换成汉语拼音,当然也有个别生僻字不能转换,如果你想转换所有的汉字拼音的话,可能需要再配合一个汉字字库来实现,使用该类文件就基本能满足你的项目需求了。用法:
复制代码 代码如下:
require_once("Pinyin.class.php");
$pinyin = new Pinyin();
$str = '好好学习天天向上';
echo $pinyin->strtopin($str);
执行后输出:hao hao xue xi tian tian xiang shang。
如果只是想输出汉字首字母则使用:
复制代码 代码如下:
echo $pinyin->strtopin($str,1);
执行后输出:hhxxttxs。

Pinning.class.php 日志:
喜欢这个喜欢:
下面的课程{
 私有 $d = 数组 (
  数组 (
   “一个”,
   -20319
  ),
  数组 (
   “哦”,
   -20317
  ),
  数组 (
   “我”,
   -20304
  ),
  数组 (
   “我”,
   -20295
  ),
  数组 (
   “是的”,
   -20292
  ),
  数组 (
   “不”,
   -20283
  ),
  数组 (
   “再见”,
   -20265
  ),
  数组 (
   “禁止”,
   -20257
  ),
  数组 (
   “砰”,
   -20242
  ),
  数组 (
   “宝”,
   -20230
  ),
  数组 (
   “价格”,
   -20051
  ),
  数组 (
   “嗯”,
   -20036
  ),
  数组 (
   “银行”,
   -20032
  ),
  数组 (
   “来吧”,
   -20026
  ),
  数组 (
   “男人”,
   -20002
  ),
  数组 (
   “生物”,
   -19990
  ),
  数组 (
   “美丽”,
   -19986
  ),
  数组 (
   “走吧”,
   -19982
  ),
  数组 (
   “宾”,
   -19976
  ),
  数组 (
   “他们”,
   -19805
  ),
  数组 (
   “血”,
   -19784
  ),
  数组 (
   “ca”,
   -19775
  ),
  数组 (
   “水”,
   -19774
  ),
  数组 (
   “可以”,
   -19763
  ),
  数组 (
   “钱”,
   -19756
  ),
  数组 (
   “混乱”,
   -19751   ),
  数组 (
   “什么”,
   -19746
  ),
  数组 (
   “太阳”,
   -19741
  ),
  数组 (
   “不”,
   -19739
  ),
  数组 (
   “茶”,
   -19728
  ),
  数组 (
   “小”,
   -19725
  ),
  数组 (
   “好”,
   -19715
  ),
  数组 (
   “超”,
   -19540
  ),
  数组 (
   “那个”,
   -19531
  ),
  数组 (
   “纯粹”,
   -19525
  ),
  数组 (
   “日”,
   -19515
  ),
  数组 (
   “谁”,
   -19500
  ),
  数组 (
   “选择”,
   -19484
  ),
  数组 (
   “卷心菜”,
   -19479
  ),
  数组 (
   “吸”,
   -19467
  ),
  数组 (
   “选择”,
   -19289
  ),
  数组 (
   “川”,
   -19288
  ),
  数组 (
   “闯”,
   -19281
  ),
  数组 (
   “崔”,
   -19275
  ),
  数组 (
   “心”,
   -19270
  ),
  数组 (
   “吸”,
   -19263
  ),
  数组 (
   “在”,
   -19261
  ),
  数组 (
   “站起来”,
   -19249
  ),
  数组 (
   “脖子”,
   -19243
  ),
  数组 (
   “cu”,
   -19242
  ),
  数组 (
   “当”,
   -19238
  ),
  数组 (
   “谁”,
   -19235
  ),
  数组 (
   “与”,
   -19227
  ),
  数组 (
   “身体”,
   -19224
  ),
  数组 (
   “肉”,
   -19218
  ),
  数组 (
   “给”,
   -19212
  ),
  数组 (
   “和”,
   -19038
  ),
  数组 (
   “当”,
   -19023
  ),
  数组 (
   “左”,
   -19018
  ),
  数组 (
   “的”,
   -19006
  ),
  数组 (
   “听”,
   -19003
  ),
  数组 (
   “说”,
   -18996
  ),
  数组 (
   “黛安”,
   -18977
  ),
  数组 (
   “恶魔”,
   -18961
  ),
  数组 (
   “死”,
   -18952
  ),
  数组 (
   “唱歌”,
   -18783
  ),
  数组 (
   “神”,
   -18774
  ),
  数组 (
   “现在”,
   -18773
  ),
  数组 (
   “二”,
   -18763
  ),
  数组 (
   “不要”,
   -18756
  ),
  数组 (
   “段”,
   -18741
  ),
  数组 (
   “二”,
   -18735
  ),
  数组 (
   “世界”,
   -18731
  ),
  数组 (
   “二重奏”,
   -18722
  ),
  数组 (
   “和”,
   -18710
  ),
  数组 (
   “它”,
   -18697
  ),
  数组 (
   “呃”,
   -18696
  ),
  数组 (
   “这里”,
   -18526
  ),
  数组 (
   “扇子”,
   -18518
  ),
  数组 (
   “方”,
   -18501
  ),
  数组 (
   “仙女”,
   -18490
  ),
  数组 (
   “谎言”,
   -18478
  ),
  数组 (
   “凤”,
   -18463
  ),
  数组 (
   “向下”,
   -18448
  ),
  数组 (
   “疯狂”,
   -18447
  ),
  数组 (
   “操”,
   -18446
  ),
  数组 (
   “不”,
   -18239
  ),
  数组 (
   “家伙”,
   -18237
  ),
  数组 (
   “客人”,
   -18231
  ),
  数组 (
   “帮派”,
   -18220
  ),
  数组 (
   “高”,
   -18211
  ),
  数组 (
   “格”,
   -18201
  ),
  数组 (
   “给”,
   -18184
  ),
  数组 (
   “根”,
   -18183
  ),
  数组 (
   “耿”,
   -18181
  ),
  数组 (
   “锣”,
   -18012
  ),
  数组 (
   “苟”,
   -17997
  ),
  数组 (
   “咕”,
   -17988
  ),
  数组 (
   “卦”,
   -17970
  ),
  数组 (
   “怪”,
   -17964
  ),
  数组 (
   “关”,
   -17961
  ),
  数组 (
   “光”,
   -17950
  ),
  数组 (
   “gui”,
   -17947
  ),
  数组 (
   “枪”,
   -17931
  ),
  数组 (
   “郭”,
   -17928
  ),
  数组 (
   “哈”,
   -17922
  ),
  数组 (
   “嗨”,
   -17759
  ),
  数组 (
   “韩”,
   -17752
  ),
  数组 (
   “挂”,
   -17733
  ),
  数组 (
   “浩”,
   -17730
  ),
  数组 (
   “他”,
   -17721
  ),
  数组 (
   “嘿”,
   -17703
  ),
  数组 (
   “母鸡”,
   -17701
  ),
  数组 (
   “哼”,
   -17697
  ),
  数组 (
   “洪”,
   -17692
  ),
  数组 (
   “侯”,
   -17683
  ),
  数组 (
   “呼”,
   -17676
  ),
  数组 (
   “华”,
   -17496
  ),
  数组 (
   “怀”,
   -17487
  ),
  数组 (
   “欢”,
   -17482
  ),
  数组 (
   “黄”,
   -17468
  ),
  数组 (
   “慧”,
   -17454
  ),
  数组 (
   “魂”,
   -17433
  ),
  数组 (
   “霍”,
   -17427
  ),
  数组 (
   “吉”,
   -17417
  ),
  数组 (
   “嘉”,
   -17202
  ),
  数组 (
   “健”,
   -17185
  ),
  数组 (
   “江”,
   -16983
  ),
  数组 (
   “娇”,
   -16970
  ),
  数组 (
   "杰",
   -16942
  ),
  数组 (
   “金”,
   -16915
  ),
  数组 (
   “静”,
   -16733
  ),
  数组 (
   “炯”,
   -16708
  ),
  数组 (
   "玖",
   -16706
  ),
  数组 (
   “ju”,
   -16689
  ),
  数组 (
   “胡安”,
   -16664
  ),
  数组 (
   “玩”,
   -16657
  ),
  数组 (
   “一”,
   -16647
  ),
  数组 (
   “但是”,
   -16474
  ),
  数组 (
   “吃”,
   -16470
  ),
  数组 (
   “对”,
   -16465
  ),
  数组 (
   “对”,
   -16459
  ),
  数组 (
   “你”,
   -16452
  ),
  数组 (
   “是”,
   -16448
  ),
  数组 (
   “仅”,
   -16433
  ),
  数组 (
   “肯”,
   -16429
  ),
  数组 (
   “孔”,
   -16427
  ),
  数组 (
   “你”,
   -16423
  ),
  数组 (
   “到”,
   -16419
  ),
  数组 (
   “核心”,
   -16412
  ),
  数组 (
   “鹌鹑”,
   -16407
  ),
  数组 (
   “玉米”,
   -16403
  ),
  数组 (
   “说”,
   -16401
  ),
  数组 (
   “爷爷”,
   -16393
  ),
  数组 (
   “一”,
   -16220
  ),
  数组 (
   “哪里”,
   -16216
  ),
  数组 (
   “它”,
   -16212
  ),
  数组 (
   “李”,
   -16205
  ),
  数组 (
   “什么”,
   -16202
  ),
  数组 (
   “仅”,
   -16187
  ),
  数组 (
   “左”,
   -16180
  ),
  数组 (
   “动物”,
   -16171
  ),
  数组 (
   “山羊”,
   -16169
  ),
  数组 (
   “空”,
   -16158
  ),
  数组 (
   “你”,
   -16155
  ),
  数组 (
   “莉亚”,
   -15959
  ),
  数组 (
   “瘦”,
   -15958
  ),
  数组 (
   “火”,
   -15944
  ),
  数组 (
   “男孩”,
   -15933
  ),
  数组 (
   “谎言”,
   -15920
  ),
  数组 (
   “瘦”,
   -15915
  ),
  数组 (
   “令”,
   -15903
  ),
  数组 (
   “回来”,
   -15889
  ),
  数组 (
   “长”,
   -15878
  ),
  数组 (
   “卢”,
   -15707
  ),
  数组 (
   “它”,
   -15701
  ),
  数组 (
   “lv”,
   -15681
  ),
  数组 (
   “鸾”,
   -15667
  ),
  数组 (
   “步行”,
   -15661
  ),
  数组 (
   “月亮”,
   -15659
  ),
  数组 (
   “洛”,
   -15652
  ),
  数组 (
   “妈”,
   -15640
  ),
  数组 (
   “麦”,
   -15631
  ),
  数组 (
   “男人”,
   -15625
  ),
  数组 (
   “芒”,
   -15454
  ),
  数组 (
   “毛”,
   -15448
  ),
  数组 (
   “我”,
   -15436
  ),
  数组 (
   “美”,
   -15435
  ),
  数组 (
   “男人”,
   -15419
  ),
  数组 (
   “萌”,
   -15416
  ),
  数组 (
   “米”,
   -15408
  ),
  数组 (
   “面”,
   -15394
  ),
  数组 (
   “喵”,
   -15385
  ),
  数组 (
   “三重”,
   -15377
  ),
  数组 (
   “分钟”,
   -15375
  ),
  数组 (
   “鸣”,
   -15369
  ),
  数组 (
   “缪”,
   -15363
  ),
  数组 (
   “莫”,
   -15362
  ),
  数组 (
   “牟”,
   -15183
  ),
  数组 (
   “穆”,
   -15180
  ),
  数组 (
   “娜”,
   -15165
  ),
  数组 (
   “奈”,
   -15158
  ),
  数组 (
   “南”,
   -15153
  ),
  数组 (
   “馕”,
   -15150
  ),
  数组 (
   “奈奥”,
   -15149
  ),
  数组 (
   “ne”,
   -15144
  ),
  数组 (
   “内”,
   -15143
  ),
  数组 (
   “nen”,
   -15141
  ),
  数组 (
   “能”,
   -15140
  ),
  数组 (
   “你”,
   -15139
  ),
  数组 (
   “年”,
   -15128
  ),
  数组 (
   “娘”,
   -15121
  ),
  数组 (
   "niao",
   -15119
  ),
  数组 (
   “涅”,
   -15117
  ),
  数组 (
   “宁”,
   -15110
  ),
  数组 (
   “宁”,
   -15109
  ),
  数组 (
   “妞”,
   -14941
  ),
  数组 (
   “农”,
   -14937
  ),
  数组 (
   “努”,
   -14933
  ),
  数组 (
   “nv”,
   -14930
  ),
  数组 (
   “暖”,
   -14929
  ),
  数组 (
   “nue”,
   -14928
  ),
  数组 (
   “诺”,
   -14926
  ),
  数组 (
   “o”,
   -14922
  ),
  数组 (
   “欧”,
   -14921
  ),
  数组 (
   “啪”,
   -14914
  ),
  数组 (
   “派”,
   -14908
  ),
  数组 (
   “平底锅”,
   -14902
  ),
  数组 (
   “嘭”,
   -14894
  ),
  数组 (
   “泡”,
   -14889
  ),
  数组 (
   “裴”,
   -14882
  ),
  数组 (
   “笔”,
   -14873
  ),
  数组 (
   “鹏”,
   -14871
  ),
  数组 (
   “pi”,
   -14857
  ),
  数组 (
   “片”,
   -14678
  ),
  数组 (
   “飘”,
   -14674
  ),
  数组 (
   “馅饼”,
   -14670
  ),
  数组 (
   “别针”,
   -14668
  ),
  数组 (
   “平”,
   -14663
  ),
  数组 (
   “噗”,
   -14654
  ),
  数组 (
   “pu”,
   -14645
  ),
  数组 (
   “气”,
   -14630
  ),
  数组 (
   “恰”,
   -14594
  ),
  数组 (
   “钱”,
   -14429
  ),
  数组 (
   “强”,
   -14407
  ),
  数组 (
   “巧”,
   -14399
  ),
  数组 (
   “切”,
   -14384
  ),
  数组 (
   “琴”,
   -14379
  ),
  数组 (
   “清”,
   -14368
  ),
  数组 (
   "琼",
   -14355
  ),
  数组 (
   “秋”,
   -14353
  ),
  数组 (
   “曲”,
   -14345
  ),
  数组 (
   “泉”,
   -14170
  ),
  数组 (
   “阙”,
   -14159
  ),
  数组 (
   “群”,
   -14151
  ),
  数组 (
   “跑”,
   -14149
  ),
  数组 (
   “响了”,
   -14145
  ),
  数组 (
   “饶”,
   -14140
  ),
  数组 (
   “重新”,
   -14137
  ),
  数组 (
   “任”,
   -14135
  ),
  数组 (
   “瑛”,
   -14125
  ),
  数组 (
   “ri”,
   -14123
  ),
  数组 (
   “荣”,
   -14122
  ),
  数组 (
   “柔”,
   -14112
  ),
  数组 (
   “茹”,
   -14109
  ),
  数组 (
   "阮",
   -14099
  ),
  数组 (
   “睿”,
   -14097
  ),
  数组 (
   “跑”,
   -14094
  ),
  数组 (
   “若”,
   -14092
  ),
  数组 (
   “萨”,
   -14090
  ),
  数组 (
   “赛”,
   -14087
  ),
  数组 (
   “桑”,
   -14083
  ),
  数组 (
   “唱”,
   -13917
  ),
  数组 (
   “肉”,
   -13914
  ),
  数组 (
   “如果”,
   -13910
  ),
  数组 (
   “钱”,
   -13907
  ),
  数组 (
   “瘦”,
   -13906
  ),
  数组 (
   “茶”,
   -13905
  ),
  数组 (
   “茶”,
   -13896
  ),
  数组 (
   “肉”,
   -13894
  ),
  数组 (
   “输入”,
   -13878
  ),
  数组 (
   “少”,
   -13870
  ),
  数组 (
   “她”,
   -13859
  ),
  数组 (
   “那个”,
   -13847
  ),
  数组 (
   “闪耀”,
   -13831
  ),
  数组 (
   “它”,
   -13658
  ),
  数组 (
   “受”,
   -13611
  ),
  数组 (
   “舒”,
   -13601
  ),
  数组 (
   “唰”,
   -13406
  ),
  数组 (
   “帅”,
   -13404
  ),
  数组 (
   “舒安”,
   -13400
  ),
  数组 (
   “爽”,
   -13398
  ),
  数组 (
   “骨头”,
   -13395
  ),
  数组 (
   “讨厌”,
   -13391
  ),
  数组 (
   “说”,
   -13387
  ),
  数组 (
   “是的”,
   -13383
  ),
  数组 (
   “歌”,
   -13367
  ),
  数组 (
   “苏”,
   -13359
  ),
  数组 (
   “名字”,
   -13356
  ),
  数组 (
   “苏安”,
   -13343
  ),
  数组 (
   “骨头”,
   -13340
  ),
  数组 (
   “太阳”,
   -13329
  ),
  数组 (
   “索”,
   -13326
  ),
  数组 (
   “是的”,
   -13318
  ),
  数组 (
   “妈的”,
   -13147
  ),
  数组 (
   “晒黑”,
   -13138
  ),
  数组 (
   “仅”,
   -13120
  ),
  数组 (
   “人们”,
   -13107
  ),
  数组 (
   “茶”,
   -13096
  ),
  数组 (
   “仍然”,
   -13095
  ),
  数组 (
   “你”,
   -13091
  ),
  数组 (
   “田”,
   -13076
  ),
  数组 (
   “伙计”,
   -13068
  ),
  数组 (
   “茶”,
   -13063
  ),
  数组 (
   “想”,
   -13060
  ),
  数组 (
   “桶”,
   -12888
  ),
  数组 (
   “你”,
   -12875
  ),
  数组 (
   “你”,
   -12871
  ),
  数组 (
   “团”,
   -12860
  ),
  数组 (
   “tui”,
   -12858
  ),
  数组 (
   “屯”,
   -12852
  ),
  数组 (
   “坨”,
   -12849
  ),
  数组 (
   “哇”,
   -12838
  ),
  数组 (
   “哇”,
   -12831
  ),
  数组 (
   “万”,
   -12829
  ),
  数组 (
   “王”,
   -12812
  ),
  数组 (
   “伟”,
   -12802
  ),
  数组 (
   “文”,
   -12607
  ),
  数组 (
   “翁”,
   -12597
  ),
  数组 (
   “我”,
   -12594
  ),
  数组 (
   “吴”,
   -12585
  ),
  数组 (
   “夕”,
   -12556
  ),
  数组 (
   “霞”,
   -12359
  ),
  数组 (
   “西安”,
   -12346
  ),
  数组 (
   “翔”,
   -12320
  ),
  数组 (
   “晓”,
   -12300
  ),
  数组 (
   “谢”,
   -12120
  ),
  数组 (
   “鑫”,
   -12099
  ),
  数组 (
   “兴”,
   -12089
  ),
  数组 (
   “兄”,
   -12074
  ),
  数组 (
   "秀",
   -12067
  ),
  数组 (
   “徐”,
   -12058
  ),
  数组 (
   “轩”,
   -12039
  ),
  数组 (
   “雪”,
   -11867
  ),
  数组 (
   “巽”,
   -11861
  ),
  数组 (
   “呀”,
   -11847
  ),
  数组 (
   “颜”,
   -11831
  ),
  数组 (
   “阳”,
   -11798
  ),
  数组 (
   “呀”,
   -11781
  ),
  数组 (
   “你们”,
   -11604
  ),
  数组 (
   “一”,
   -11589
  ),
  数组 (
   “阴”,
   -11536
  ),
  数组 (
   “莹”,
   -11358
  ),
  数组 (
   “哟”,
   -11340
  ),
  数组 (
   “勇”,
   -11339
  ),
  数组 (
   “你”,
   -11324
  ),
  数组 (
   “于”,
   -11303
  ),
  数组 (
   “元”,
   -11097
  ),
  数组 (
   “悦”,
   -11077
  ),
  数组 (
   “云”,
   -11067
  ),
  数组 (
   “扎”,
   -11055
  ),
  数组 (
   “哉”,
   -11052
  ),
  数组 (
   “赞”,
   -11045
  ),
  数组 (
   “藏”,
   -11041
  ),
  数组 (
   “早”,
   -11038
  ),
  数组 (
   “泽”,
   -11024
  ),
  数组 (
   “泽伊”,
   -11020
  ),
  数组 (
   “禅”,
   -11019
  ),
  数组 (
   “曾”,
   -11018
  ),
  数组 (
   “扎”,
   -11014
  ),
  数组 (
   “斋”,
   -10838
  ),
  数组 (
   “湛”,
   -10832
  ),
  数组 (
   “张”,
   -10815
  ),
  数组 (
   “赵”,
   -10800
  ),
  数组 (
   “喆”,
   -10790
  ),
  数组 (
   “臻”,
   -10780
  ),
  数组 (
   “郑”,
   -10764
  ),
  数组 (
   “芝”,
   -10587
  ),
  数组 (
   “中”,
   -10544
  ),
  数组 (
   “周”,
   -10533
  ),
  数组 (
   “朱”,
   -10519
  ),
  数组 (
   “抓”,
   -10331
  ),
  数组 (
   “抓”,
   -10329
  ),
  数组 (
   "转",
   -10328
  ),
  数组 (
   “庄”,
   -10322
  ),
  数组 (
   “追”,
   -10315
  ),
  数组 (
   “准”,
   -10309
  ),
  数组 (
   “卓”,
   -10307
  ),
  数组 (
   “子”,
   -10296
  ),
  数组 (
   “宗”,
   -10281
  ),
  数组 (
   “邹”,
   -10274
  ),
  数组 (
   “祖”,
   -10270
  ),
  数组 (
   “祖安”,
   -10262
  ),
  数组 (
   “zui”,
   -10260
  ),
  数组 (
   “尊”,
   -10256
  ),
  数组 (
   “左”,
   -10254
  )
 );

 函数 g($num) {
  if ($num > 0 && $num    返回 chr($num);
  }
  elseif ($num -10247) {
   返回“”;
  } 其他 {
   for ($i = count($this->d) - 1; $i >= 0; $i--) {
    if ($this->d[$i][1]      休息;
   }
   返回 $this->d[$i][0];
  }
 }

 函数 c($str) {
  $ret = "";
  for ($i = 0; $i    $p = ord(substr($str, $i, 1));

   如果 ($p > 160) {

    $q = ord(substr($str, $i, 1));

    $p = $p * 256 $q -65536;

   }
   $ret .= $this->g($p);

  }
  $ret = substr($ret, 0, 1);
  返回 $ret;
 }

 function f($str) {
  $ret = "";
  for ($i = 0; $i < strlen($str); $i ) {
   $p = ord(substr($str, $i, 1));
   if ($p > 160) {
    $q = ord(substr($str, $i, 1));
    $p = $p * 256 $q -65536;
   }
   $ret .= $this->g($p);
  }
  return $ret;
 }

 /*
 PHP截取UTF-8编码的中英文字符串
 */
 function utf8_substr($str, $from, $len) {
  return preg_replace('#^(?:[x00-x7F]|[xC0-xFF][x80-xBF] ){0,' . $from . '}' . '((?:[x00-x7F]|[xC0-xFF][x80-xBF] ){0,' . $len . '}).*#s', '$1', $str);
 }

 /*
 将中英文字符串转换成拼音
 */
 function strtopin($chinese,$type=0) {
  $i = 0;
  $pinyin = "";
  $arr = array ();
  while (1) {
   $str = $this->utf8_substr($chinese, $i, 1);
   if (!empty ($str)) {
    $arr[$i] = $str;
    $str = iconv("utf-8", "gb2312", $str);
    if($type==1){ //转换成首字母
     $pinyin .= $this->c($str);
    }else{     //转换成全拼
     $pinyin .= $this->f($str)." ";
    }
    $i = $i 1;
   } else
    break;
  }
  return $pinyin;
 }
}


?>

2、PHP公历农历转换
公历转农历的算法非常复杂,涉及到农历润月、干支纪年、生肖等。网络上有很多提供的算法,本文整理的一个Lunar.class.php可以实现公农历互转功能。用法:
复制代码 代码如下:
require_once("Lunar.class.php");
$lunar = new Lunar();
 
$date = $lunar->convertLunarToSolar(2013,8,17); //公历转农历
print_r($date);
执行后输出数组:
复制代码 代码如下:
Array ( [0] => 二零一三 [1] => 七月 [2] => 十一 [3] => 癸巳 
[4] => 7 [5] => 11 [6] => 蛇 [7] => 0 ) 
如果要将农历转换成公历则使用:
复制代码 代码如下:
$date = $lunar->convertLunarToSolar(2013,8,17); //农历转公历
print_r($date);
执行后输出数组:
复制代码 代码如下:
Array ( [0] => 2013 [1] => 09 [2] => 21 ) 

Lunar.class.php源码:
复制代码代码如下:
农历类 {
   var $MIN_YEAR = 1891;
    var $MAX_YEAR = 2100;
    var $lunarInfo = 数组(
        数组(0,2,9,21936),数组(6,1,30,9656),数组(0,2,17,9584),数组(0,2,6,21168),数组(5,1, 26,43344),数组(0,2,13,59728),
        数组(0,2,2,27296),数组(3,1,22,44368),数组(0,2,10,43856),数组(8,1,30,19304),数组(0,2, 19,19168),数组(0,2,8,42352),
        数组(5,1,29,21096),数组(0,2,16,53856),数组(0,2,4,55632),数组(4,1,25,27304),数组(0,2, 13,22176),数组(0,2,2,39632),
        数组(2,1,22,19176),数组(0,2,10,19168),数组(6,1,30,42200),数组(0,2,18,42192),数组(0,2, 6,53840),数组(5,1,26,54568),
        数组(0,2,14,46400),数组(0,2,3,54944),数组(2,1,23,38608),数组(0,2,11,38320),数组(7,2, 1,18872),数组(0,2,20,18800),
        数组(0,2,8,42160),数组(5,1,28,45656),数组(0,2,16,27216),数组(0,2,5,27968),数组(4,1, 24,44456),数组(0,2,13,11104),
        数组(0,2,2,38256),数组(2,1,23,18808),数组(0,2,10,18800),数组(6,1,30,25776),数组(0,2, 17,54432),数组(0,2,6,59984),
        数组(5,1,26,27976),数组(0,2,14,23248),数组(0,2,4,11104),数组(3,1,24,37744),数组(0,2, 11,37600),数组(7,1,31,51560),
        数组(0,2,19,51536),数组(0,2,8,54432),数组(6,1,27,55888),数组(0,2,15,46416),数组(0,2, 5,22176),数组(4,1,25,43736),
        数组(0,2,13,9680),数组(0,2,2,37584),数组(2,1,22,51544),数组(0,2,10,43344),数组(7,1, 29,46248),数组(0,2,17,27808),
        数组(0,2,6,46416),数组(5,1,27,21928),数组(0,2,14,19872),数组(0,2,3,42416),数组(3,1, 24,21176),数组(0,2,12,21168),
        数组(8,1,31,43344),数组(0,2,18,59728),数组(0,2,8,27296),数组(6,1,28,44368),数组(0,2, 15,43856),数组(0,2,5,19296),
        数组(4,1,25,42352),数组(0,2,13,42352),数组(0,2,2,21088),数组(3,1,21,59696),数组(0,2, 9,55632),数组(7,1,30,23208),
        数组(0,2,17,22176),数组(0,2,6,38608),数组(5,1,27,19176),数组(0,2,15,19152),数组(0,2, 3,42192),数组(4,1,23,53864),
        数组(0,2,11,53840),数组(8,1,31,54568),数组(0,2,18,46400),数组(0,2,7,46752),数组(6,1, 28,38608),数组(0,2,16,38320),
        数组(0,2,5,18864),数组(4,1,25,42168),数组(0,2,13,42160),数组(10,2,2,45656),数组(0,2, 20,27216),数组(0,2,9,27968),
        数组(6,1,29,44448),数组(0,2,17,43872),数组(0,2,6,38256),数组(5,1,27,18808),数组(0,2, 15,18800),数组(0,2,4,25776),
        数组(3,1,23,27216),数组(0,2,10,59984),数组(8,1,31,27432),数组(0,2,19,23232),数组(0,2, 7,43872),数组(5,1,28,37736),
        数组(0,2,16,37600),数组(0,2,5,51552),数组(4,1,24,54440),数组(0,2,12,54432),数组(0,2, 1,55888),数组(2,1,22,23208),
        数组(0,2,9,22176),数组(7,1,29,43736),数组(0,2,18,9680),数组(0,2,7,37584),数组(5,1, 26,51544),数组(0,2,14,43344),
        数组(0,2,3,46240),数组(4,1,23,46416),数组(0,2,10,44368),数组(9,1,31,21928),数组(0,2, 19,19360),数组(0,2,8,42416),
        数组(6,1,28,21176),数组(0,2,16,21168),数组(0,2,5,43312),数组(4,1,25,29864),数组(0,2, 12,27296),数组(0,2,1,44368),
        数组(2,1,22,19880),数组(0,2,10,19296),数组(6,1,29,42352),数组(0,2,17,42208),数组(0,2, 6,53856),数组(5,1,26,59696),
        数组(0,2,13,54576),数组(0,2,3,23200),数组(3,1,23,27472),数组(0,2,11,38608),数组(11,1, 31,19176),数组(0,2,19,19152),
        数组(0,2,8,42192),数组(6,1,28,53848),数组(0,2,15,53840),数组(0,2,4,54560),数组(5,1, 24,55968),数组(0,2,12,46496),
        数组(0,2,1,22224),数组(2,1,22,19160),数组(0,2,10,18864),数组(7,1,30,42168),数组(0,2, 17,42160),数组(0,2,6,43600),
        数组(5,1,26,46376),数组(0,2,14,27936),数组(0,2,2,44448),数组(3,1,23,21936),数组(0,2, 11,37744),数组(8,2,1,18808),
        数组(0,2,19,18800),数组(0,2,8,25776),数组(6,1,28,27216),数组(0,2,15,59984),数组(0,2, 4,27424),数组(4,1,24,43872),
        数组(0,2,12,43744),数组(0,2,2,37600),数组(3,1,21,51568),数组(0,2,9,51552),数组(7,1, 29,54440),数组(0,2,17,54432),
        数组(0,2,5,55888),数组(5,1,26,23208),数组(0,2,14,22176),数组(0,2,3,42704),数组(4,1, 23,21224),数组(0,2,11,21200),
        数组(8,1,31,43352),数组(0,2,19,43344),数组(0,2,7,46240),数组(6,1,27,46416),数组(0,2, 15,44368),数组(0,2,5,21920),
        数组(4,1,24,42448),数组(0,2,12,42416),数组(0,2,2,21168),数组(3,1,22,43320),数组(0,2, 9,26928),数组(7,1,29,29336),
        数组(0,2,17,27296),数组(0,2,6,44368),数组(5,1,26,19880),数组(0,2,14,19296),数组(0,2, 3,42352),数组(4,1,24,21104),
        数组(0,2,10,53856),数组(8,1,30,59696),数组(0,2,18,54560),数组(0,2,7,55968),数组(6,1, 27,27472),数组(0,2,15,22224),
        数组(0,2,5,19168),数组(4,1,25,42216),数组(0,2,12,42192),数组(0,2,1,53584),数组(2,1, 21,55592),数组(0,2,9,54560)
    );
    /**
     * 将阳历转换为阴历
     * @param year 公历-年
     * @param month 公历-月
     * @param date 公历-日
    */
    函数convertSolarToLunar($年,$月,$日期){
        //调试器;
        $yearData = $this->lunarInfo[$year-$this->MIN_YEAR];
        if($year==$this->MIN_YEAR&&$month             return array(1891,'正月','初一','辛卯',1,1,'兔');
        }
        return $this->getLunarByBetween($year,$this->getDaysBetweenSolar($year,$month,$date,$yearData[1],$yearData[2]));
    }

    函数convertSolarMonthToLunar($year,$month) {
        $yearData = $this->lunarInfo[$year-$this->MIN_YEAR];
        if($year==$this->MIN_YEAR&&$month             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             $array = $this->getLunarByBetween($year,$this->getDaysBetweenSolar($year, $month, $i, $yearData[1], $yearData[2]));
            $array[] = $year 。 '-'。 $月。 '-'。 $i;
            $lunar_ary[$i] = $array;
        }
        返回 $lunar_ary;
    }
    /**
    * 将阴历转换为阳历
    * @param year 阴历-年
    * @param month 阴历-月,闰月处理:例如如果当年闰五月,那么第二个五月就传六月,相当于阴历有13个月,只是有的时候第13个月的天数为0
    * @param date 阴历-日
   */
    函数convertLunarToSolar($年,$月,$日期){
        $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    = 爆炸('-', $res);
        $年    = $日[0];
        $月= $日[1];
        $day    = $day[2];
        返回数组($年, $月, $日);
    }
    /**
     * 判断是否是闰年
     * @param year
    */
    函数 isLeapYear($year){
        return (($year%4==0 && $year 0 !=0) || ($year@0==0));
    }
    /**
     * 获取干支纪年
     * @param year
    */
    函数 getLunarYearName($year){
         $sky = array('庚','辛','壬','癸','甲','乙','丙','丁','戊','己');
         $earth = array('申','酉','戌','亥','子','丑','寅','卯','辰','巳','午','未');
         $year = $year.'';
         返回 $sky[$year{3}].$earth[$year ];
    }
    /**
     * 根据阴历年获取生肖
     * @param year 阴历年
    */
    函数 getYearZodiac($year){
         $zodiac = array('猴','鸡','狗','猪','鼠','牛','虎','兔','龙','蛇','马','羊');
         返回 $zodiac[$year ];
    }
    /**
     * 获取阳历月份的天数
     * @param year 阳历-年
     * @param month 阳历-月
    */
    函数 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 阴历-月,从一月开始
    */
    函数 getLunarMonthDays($year,$month){
         $monthData = $this->getLunarMonths($year);
         返回 $monthData[$month-1];
    }
    /**
     * 获取阴历每月的天数的数组
     * @param year
    */
    函数 getLunarMonths($year){
        $yearData = $this->lunarInfo[$year - $this->MIN_YEAR];
        $leapMonth = $yearData[0];
        $bit = decbin($yearData[3]);
        for ($i = 0; $i             $bitArray[$i] = substr($bit, $i, 1);
        }
        for($k=0,$klen=16-count($bitArray);$k             array_unshift($bitArray, '0');
        }
        $bitArray = array_slice($bitArray,0,($leapMonth==0?12:13));
        for($i=0; $i             $bitArray[$i] = $bitArray[$i] 29;
        }
        返回 $bitArray;
    }
    /**
     * 获取农历每年的天数
     * @param year 农历年份
    */
    函数 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]);
    }
    函数 getLunarYearMonths($year){
         //调试器;
        $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             $temp=0;
            for($j=0;$j                 $temp =$monthData[$j];
            }
            array_push($res, $temp);
        }
         返回 $res;
    }
    /**
     * 获取闰月
     * @param year 阴历年份
    */
    函数 getLeapMonth($year){
        $yearData = $this->lunarInfo[$year-$this->MIN_YEAR];
        返回$yearData[0];
    }
    /**
     * 计算阴历日期与正月初一相隔的天数
     * @参数年份
     * @参数月份
     * @参数日期
     */
    函数 getDaysBetweenLunar($年,$月,$日期){
        $y

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn