
01.<?php02.header('Access-Control-Allow-Origin: *');03.header("Content-Type:text/javascript;charset=utf-8");04./**05.* 获取用户真实 IP06.*/07.function getIP()08.{09.static $realip;10.if (isset($_SERVER)){11.if (isset($_SERVER["HTTP_X_FORWARDED_FOR"])){12.$realip = $_SERVER["HTTP_X_FORWARDED_FOR"];13.} else if (isset($_SERVER["HTTP_CLIENT_IP"])) {14.$realip = $_SERVER["HTTP_CLIENT_IP"];15.} else {16.$realip = $_SERVER["REMOTE_ADDR"];17.}18.} else {19.if (getenv("HTTP_X_FORWARDED_FOR")){20.$realip = getenv("HTTP_X_FORWARDED_FOR");21.} else if (getenv("HTTP_CLIENT_IP")) {22.$realip = getenv("HTTP_CLIENT_IP");23.} else {24.$realip = getenv("REMOTE_ADDR");25.}26.}27. 28. 29.return $realip;30.}31. 32. 33./**34.* 获取 IP 地理位置35.* 淘宝IP接口36.* @Return: array37.*/38.function getCity($ip)39.{40.$url="http://ip.taobao.com/service/getIpInfo.php?ip=".$ip;41.$ip=json_decode(file_get_contents($url)); 42.if((string)$ip->code=='1'){43.return false;44.}45.$data = $ip->data->city;46.return $data; 47.}48. 49.function getWeather($cityId)50.{51.$url="http://m.weather.com.cn/data/".$cityId.".html";52.$weather=json_decode(file_get_contents($url));53.return $weather; 54.}55.function json_to_array($web){56.$arr=array();57.foreach($web as $k=>$w){58.if(is_object($w)) $arr[$k]=json_to_array($w); //判断类型是不是object59.else $arr[$k]=$w;60.}61.return $arr;62.}63.$city = getCity(getIP());64.$city = str_split($city,strlen($city)-3);65.$city =$city[0];66.$cityUrl = "http://evenle.com/wei/20130921/city.php";67.$web=json_decode(file_get_contents($cityUrl));68.$arr=json_to_array($web);69.$weatherInfo = getWeather($arr[$city]);70.echo('weather(');71.echo(json_encode($weatherInfo));72.echo(")");73. 74.?>01.<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">02.<html xmlns="http://www.w3.org/1999/xhtml">03.<head>04.<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />05.<title>无标题文档</title>06.<link rel="stylesheet" href="main.css" type="text/css" />07.</head>08. 09.<body>10.<div class="top">11.<div class="top_content">12.<div id="calendarArea"> <span class="datepic"> <font id="weekday"></font> </span>13.<ul>14.<li> <span id="lunarday"></span> </li>15.<li> <span id="dateday"></span> </li>16.</ul>17.</div>18.<div id="topWeatherArea">19.<ul class="cityWrap">20.<li class="name">21.<span class="name"></span>22.</li>23.<li>24.<span class="info"></span>25.</li>26.</ul>27.<span class="weapic"></span>28.<ul class="cityInfo">29.<li class="name">30.<span class="name"></span>31.</li>32.<li>33.<span class="info"></span>34.</li>35.</ul>36.</div>37.</div>38.</div>39.<script type="text/javascript" src="../../js/jquery.js"></script> 40.<script type="text/javascript" src="../../js/loadjs.js"></script> 41.<script type="text/javascript">42.var weatherInfo;43.var lunar;44.var date = new Date();45.var g={"晴":"0 0px","多云":"0 -40px","暴雪":"0 -480px","大雪-暴雪":"0 -480px","暴雨":"0 -360px","大雨-暴雨":"0 -360px","暴雨-大暴雨":"0 -360px","大暴雨-特大暴雨":"0 -360px","大暴雪":"0 -480px","飑":"0 -880px","大雪":"0 -440px","中雪-大雪":"0 -440px","大雨":"0 -320px","中雨-大雨":"0 -320px","浮层":"0 -920px","雷阵雨":"0 -160px","雷阵雨并伴有冰雹":"0 -200px","霾":"0 -920px","轻雾":"0 -840px","雾":"0 -840px","冰雹":"0 -680px","沙尘暴":"0 -760px","强沙尘暴":"0 -760px","特大暴雪":"0 -520px","小雪":"0 -600px","小雨":"0 -240px","扬沙":"0 -720px","浮尘":"0 -920px","龙卷风":"0 -800px","弱高吹雪":"0 -600px","阴":"0 -80px","雨夹雪":"0 -400px","冻雨":"0 -960px","阵雪":"0 -560px","阵雨":"0 -120px","中雪":"0 -640px","小雪-中雪":"0 -640px","小雨-中雨":"0 -280px","中雨":"0 -280px"};46.$(document).ready(function() {47.$("#weekday").html(date.getDate());48.$.getScript("getWeather.php");49.$.getScript("lunar.php");50.});51.var weather = function(data){52.weatherInfo = data;53.$(".cityWrap .name").html(data.weatherinfo.city);54.$(".cityInfo .name").html(data.weatherinfo.weather1);55.$(".cityWrap .info").html(data.weatherinfo.city_en);56.$(".cityInfo .info").html(data.weatherinfo.st1+"℃");57.$(".weapic").css("background-position",g[data.weatherinfo.weather1]).show();58.}59.var lunar = function(data){60.lunar = data;61.var month = data.month;62.var day = data.day;63.$("#lunarday").html("农历"+month+day);64.$("#dateday").html(date.getFullYear()+"."+(date.getMonth()+1)+"."+date.getDate()+" "+getWeek(date.getDay()));65.}66.var getWeek = function(day){67.switch(day){68.case 0:69.return "周一";70.case 1:71.return "周二";72.case 2:73.return "周三";74.case 3:75.return "周四";76.case 4:77.return "周五";78.case 5:79.return "周六";80.case 6:81.return "周日"; 82.}83.}84.</script>85.</body>86.</html>001.@charset "utf-8";002.* {003.padding: 0;004.margin: 0;005.border: none;006.text-decoration: none;007.list-style: none;008.overflow: hidden;009.}010.input, button, select, textarea {011.outline: none;012.}013.textarea {014.font-size: 13px;015.resize: none;016.}017.body {018.background: #F2F2F2 url(http://imghao.qq.com/haoqq/v101/v1img/xs-bg.png) top center no-repeat;019.}020..top {021.width: 100%;022.height: 65px;023.}024..top_content {025.width: 960px;026.height: 65px;027.margin: 0 auto;028.}029.#calendarArea {030.width: 155px;031.height: 65px;032.float: left;033.cursor: pointer;034.padding-left: 15px;035.}036.#calendarArea:hover, #topWeatherArea:hover {037.background: url(http://imghao.qq.com/haoqq/v101/v1img/top-hover.png) top right;038.}039..datepic {040.background-image: url(http://imghao.qq.com/haoqq/v101/v1img/pur-sprite.png);041.background-repeat: no-repeat;042.width: 31px;043.height: 32px;044.float: left;045.text-align: center;046.font-size: 16px;047.font-weight: bold;048.line-height: 32px;049.display: block;050.margin: 15px 10px 0 0;051.float: left;052.}053.#weekday {054.position: relative;055.top: 4px;056.color: #fff;057.}058.#calendarArea ul {059.float: left;060.color: #fff;061.height: 40px;062.padding-top: 13px;063.padding-left: 3px;064.}065.#calendarArea #lunarday {066.font-size: 16px;067.font-weight: bold;068.}069.#calendarArea #dateday {070.font-size: 14px;071.}072.#topWeatherArea {073.width: 165px;074.height: 65px;075.float: left;076.cursor: pointer;077.color: #fff;078.padding-left:10px;079.}080..cityWrap {081.float: left;082.margin-top: 15px;083.padding-left:5px;084.}085..weapic {086.width: 40px;087.height: 40px;088.background-image: url(http://imghao.qq.com/haoqq/v101/v1img/pur-sprite-wea.png);089.float: left;090.display: block;091.margin: 15px 5px 0 5px;092.background-repeat: no-repeat;093.display:none;094.}095..cityInfo {096.float: left;097.margin-top: 15px;098.}099..name{100.font-weight: bold;101.font-size:17px;102.}103..info{104.font-size:14px;105.}001.<?php002.class Lunar {003.public $MIN_YEAR = 1891;004.public $MAX_YEAR = 2100;005.public $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));006. 007./**008.* 将阳历转换为阴历009.* @param year 公历-年010.* @param month 公历-月011.* @param date 公历-日012.*/013.function convertSolarToLunar($year,$month,$date){014.//debugger;015.$yearData = $this->lunarInfo[$year-$this->MIN_YEAR];016.if($year==$this->MIN_YEAR&&$month<=2&&$date<=9){017.return array(1891,'正月','初一','辛卯',1,1,'兔');018.}019.return $this->getLunarByBetween($year,$this->getDaysBetweenSolar($year,$month,$date,$yearData[1],$yearData[2]));020.}021. 022./**023.* 将阴历转换为阳历024.* @param year 阴历-年025.* @param month 阴历-月,闰月处理:例如如果当年闰五月,那么第二个五月就传六月,相当于阴历有13个月,只是有的时候第13个月的天数为0026.* @param date 阴历-日027.*/028.function convertLunarToSolar($year,$month,$date){029.$yearData = $this->lunarInfo[$year-$this->MIN_YEAR];030.$between = $this->getDaysBetweenLunar($year,$month,$date);031.$res = mktime(0,0,0,$yearData[1],$yearData[2],$year);032. 033.$res = date('Y-m-d',$res+$between*24*60*60);034. 035.$day = split_date($res);036.$year = $day[0];037.$month= $day[1];038.$day = $day[2];039. 040.return array($year, $month, $day);041.}042. 043./**044.* 判断是否是闰年045.* @param year046.*/047.function isLeapYear($year){048.return (($year%4==0 && $year%100 !=0) || ($year%400==0));049.}050. 051./**052.* 获取干支纪年053.* @param year054.*/055.function getLunarYearName($year){056.$sky = array('庚','辛','壬','癸','甲','乙','丙','丁','戊','己');057.$earth = array('申','酉','戌','亥','子','丑','寅','卯','辰','巳','午','未');058. 059.$year = $year.'';060.return $sky[$year{3}].$earth[$year%12];061.}062. 063./**064.* 根据阴历年获取生肖065.* @param year 阴历年066.*/067.function getYearZodiac($year){068.$zodiac = array('猴','鸡','狗','猪','鼠','牛','虎','兔','龙','蛇','马','羊');069.return $zodiac[$year%12];070.}071. 072./**073.* 获取阳历月份的天数074.* @param year 阳历-年075.* @param month 阳历-月076.*/077.function getSolarMonthDays($year,$month){078.$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);079.return $monthHash["$month"];080.}081. 082. 083./**084.* 获取阴历月份的天数085.* @param year 阴历-年086.* @param month 阴历-月,从一月开始087.*/088.function getLunarMonthDays($year,$month){089.$monthData = $this->getLunarMonths($year);090.return $monthData[$month-1];091.}092. 093./**094.* 获取阴历每月的天数的数组095.* @param year096.*/097.function getLunarMonths($year){098. 099.$yearData = $this->lunarInfo[$year - $this->MIN_YEAR];100.$leapMonth = $yearData[0];101. 102.$bit = decbin($yearData[3]);103.for ($i = 0; $i < strlen($bit);$i ++) {104.$bitArray[$i] = substr($bit, $i, 1);105.}106. 107.for($k=0,$klen=16-count($bitArray);$k<$klen;$k++){108.array_unshift($bitArray, '0');109.}110. 111.$bitArray = array_slice($bitArray,0,($leapMonth==0?12:13));112.for($i=0; $i<count($bitArray); $i++){113.$bitArray[$i] = $bitArray[$i] + 29;114.}115. 116.return $bitArray;117.}118. 119./**120.* 获取农历每年的天数121.* @param year 农历年份122.*/123.function getLunarYearDays($year){124.$yearData = $this->lunarInfo[$year-$this->MIN_YEAR];125.$monthArray = $this->getLunarYearMonths($year);126.$len = count($monthArray);127.return ($monthArray[$len-1]==0?$monthArray[$len-2]:$monthArray[$len-1]);128.}129. 130.function getLunarYearMonths($year){131.//debugger;132.$monthData = $this->getLunarMonths($year);133.$res=array();134.$temp=0;135.$yearData = $this->lunarInfo[$year-$this->MIN_YEAR];136.$len = ($yearData[0]==0?12:13);137.for($i=0;$i<$len;$i++){138.$temp=0;139.for($j=0;$j<=$i;$j++){140.$temp+=$monthData[$j];141.}142.array_push($res, $temp);143.}144.return $res;145.}146. 147./**148.* 获取闰月149.* @param year 阴历年份150.*/151.function getLeapMonth($year){152.$yearData = $this->lunarInfo[$year-$this->MIN_YEAR];153.return $yearData[0];154.}155. 156./**157.* 计算阴历日期与正月初一相隔的天数158.* @param year159.* @param month160.* @param date161.*/162.function getDaysBetweenLunar($year,$month,$date){163.$yearMonth = $this->getLunarMonths($year);164. 165.$res=0;166.for($i=1;$i<$month;$i++){167.$res +=$yearMonth[$i-1];168.}169.$res+=$date-1;170. 171.return $res;172.}173. 174./**175.* 计算2个阳历日期之间的天数176.* @param year 阳历年177.* @param cmonth178.* @param cdate179.* @param dmonth 阴历正月对应的阳历月份180.* @param ddate 阴历初一对应的阳历天数181.*/182.function getDaysBetweenSolar($year,$cmonth,$cdate,$dmonth,$ddate){183. 184.$a = mktime(0,0,0,$cmonth,$cdate,$year);185.$b = mktime(0,0,0,$dmonth,$ddate,$year);186. 187.return ceil(($a-$b)/24/3600);188.}189. 190./**191.* 根据距离正月初一的天数计算阴历日期192.* @param year 阳历年193.* @param between 天数194.*/195.function getLunarByBetween($year,$between){196.//debugger;197.$lunarArray = array();198.$yearMonth=array();199.$t=0;200.$e=0;201.$leapMonth=0;202.$m='';203. 204.if($between==0){205.array_push($lunarArray, $year,'正月','初一');206.$t = 1;207.$e = 1;208.}else{209.$year = $between>0? $year : ($year-1);210.$yearMonth = $this->getLunarYearMonths($year);211.$leapMonth = $this->getLeapMonth($year);212.$between = $between>0?$between : ($this->getLunarYearDays($year)+$between);213. 214.for($i=0;$i<13;$i++){215.if($between==$yearMonth[$i]){216.$t=$i+2;217.$e=1;218.break;219.}else if($between<$yearMonth[$i]){220.$t=$i+1;221.$e=$between-(emptyempty($yearMonth[$i-1])?0:$yearMonth[$i-1])+1;222.break;223.}224.}225.$m = ($leapMonth!=0&&$t==$leapMonth+1)?('闰'.$this->getCapitalNum($t- 1,true)):$this->getCapitalNum(($leapMonth!=0&&$leapMonth+1<$t?($t-1):$t),true);226. 227.array_push($lunarArray,$year,$m,$this->getCapitalNum($e,false));228.}229.array_push($lunarArray,$this->getLunarYearName($year));//天干地支230.array_push($lunarArray,$t,$e);231.array_push($lunarArray,$this->getYearZodiac($year));//12生肖232.array_push($lunarArray,$leapMonth);//闰几月233.return $lunarArray;234.}235. 236./**237.* 获取数字的阴历叫法238.* @param num 数字239.* @param isMonth 是否是月份的数字240.*/241.function getCapitalNum($num,$isMonth){242.$isMonth = $isMonth || false;243.$dateHash=array('0'=>'','1'=>'一','2'=>'二','3'=>'三','4'=>'四','5'=>'五','6'=>'六','7'=>'七','8'=>'八','9'=>'九','10'=>'十 ');244.$monthHash=array('0'=>'','1'=>'正月','2'=>'二月','3'=>'三月','4'=>'四月','5'=>'五月','6'=>'六月','7'=>'七月','8'=>'八月','9'=>'九月','10'=>'十月','11'=>'冬月','12'=>'腊月');245.$res='';246. 247.if($isMonth){248.$res = $monthHash[$num];249.}else{250.if($num<=10){251.$res = '初'.$dateHash[$num];252.}else if($num>10&&$num<20){253.$res = '十'.$dateHash[$num-10];254.}else if($num==20){255.$res = "二十";256.}else if($num>20&&$num<30){257.$res = "廿".$dateHash[$num-20];258.}else if($num==30){259.$res = "三十";260.}261.}262.return $res;263.}264. 265.}266.?>267.<?php268.header('Access-Control-Allow-Origin: *');269.header("Content-Type:text/javascript;charset=utf-8");270.$lunar = new Lunar();271.$year = date("Y");272.$month = date("n");273.$day = date("j");274.$ldate=$lunar->convertSolarToLunar($year, $month, $day);275.echo('lunar({"year":"'.$ldate[0].'","month":"'.$ldate[1].'","day":"'.$ldate[2].'","zodiac":"'.$ldate[6].'","sign":"'.$ldate[3].'"})');276.?>