一个php 根据生日计算星座和生肖程序,有需要的朋友可参考参考.
魔羯座(12/22 – 1/19)、水瓶座(1/20 – 2/18)、双鱼座(2/19 – 3/20)、牡羊座(3/21 – 4/20)、金牛座(4/21 – 5/20)、双子座(5/21 – 6/21)、巨蟹座(6/22 – 7/22)、狮子座(7/23 – 8/22)、处女座(8/23 – 9/22)、天秤座(9/23 – 10/22)、天蝎座(10/23 – 11/21)、射手座(11/22 – 12/21)
PHP实例代码如下:
- /**
- *getConstellation根据出生生日取得星座
- *
- *@paramString$brithday用于得到星座的日期格式为yyyy-mm-dd
- *
- *@paramArray$format用于返回星座的名称
- *
- *@returnString
- */
- functiongetConstellation($birthday,$format=null)
- {
- $pattern=‘/^d{4}-d{1,2}-d{1,2}$/’;
- if(!preg_match($pattern,$birthday,$matchs))
- {
- returnnull;
- }
- $date=explode(‘-’,$birthday);
- $year=$date[0];
- $month=$date[1];
- $day=$date[2];
- if($month<1||$month>12||$day<1||$day>31)
- {
- returnnull;
- }
- //设定星座数组
- $constellations=array(
- ‘摩羯座’,‘水瓶座’,‘双鱼座’,‘白羊座’,‘金牛座’,‘双子座’,
- ‘巨蟹座’,'狮子座’,‘处女座’,‘天秤座’,‘天蝎座’,‘射手座’,);
- //或$constellations=array(
- ‘Capricorn’,‘Aquarius’,‘Pisces’,‘Aries’,‘Taurus’,‘Gemini’,
- ‘Cancer’,'Leo’,‘Virgo’,‘Libra’,‘Scorpio’,‘Sagittarius’,);
- //设定星座结束日期的数组,用于判断
- $enddays=array(19,18,20,20,20,21,22,22,22,22,21,21,);
- //如果参数format被设置,则返回值采用format提供的数组,否则使用默认的数组
- if($format!=null)
- {
- $html' target='_blank'>values=$format;
- }
- else
- {
- $values=$constellations;
- }
- //根据月份和日期判断星座
- switch($month)
- {
- case1:
- if($day<=$enddays[0])
- {
- $constellation=$values[0];
- }
- else
- {
- $constellation=$values[1];
- }
- break;
- case2:
- if($day<=$enddays[1])
- {
- $constellation=$values[1];
- }
- else
- {
- $constellation=$values[2];
- }
- break;
- case3:
- if($day<=$enddays[2])
- {
- $constellation=$values[2];
- }
- else
- {
- $constellation=$values[3];
- }
- break;
- case4:
- if($day<=$enddays[3])
- {
- $constellation=$values[3];
- }
- else
- {
- $constellation=$values[4];
- }
- break;
- case5:
- if($day<=$enddays[4])
- {
- $constellation=$values[4];
- }
- else
- {
- $constellation=$values[5];
- }
- break;
- case6:
- if($day<=$enddays[5])
- {
- $constellation=$values[5];
- }
- else
- {
- $constellation=$values[6];
- }
- break;
- case7:
- if($day<=$enddays[6])
- {
- $constellation=$values[6];
- }
- else
- {
- $constellation=$values[7];
- }
- break;
- case8:
- if($day<=$enddays[7])
- {
- $constellation=$values[7];
- }
- else
- {
- $constellation=$values[8];
- }
- break;
- case9:
- if($day<=$enddays[8])
- {
- $constellation=$values[8];
- }
- else
- {
- $constellation=$values[9];
- }
- break;
- case10:
- if($day<=$enddays[9])
- {
- $constellation=$values[9];
- }
- else
- {
- $constellation=$values[10];
- }
- break;
- case11:
- if($day<=$enddays[10])
- {
- $constellation=$values[10];
- }
- else
- {
- $constellation=$values[11];
- }
- break;
- case12:
- if($day<=$enddays[11])
- {
- $constellation=$values[11];
- }
- else
- {
- $constellation=$values[0];
- }
- break;
- }
- return$constellation;
- }
- js格式的:
- 根据生日的月份和日期,计算星座的js小函数(最简)
- //根据生日的月份和日期,计算星座。http://blog.111cn.net/cuixiping/
- functiongetAstro(month,day){
- vars=”魔羯水瓶双鱼牡羊金牛双子巨蟹狮子处女天秤天蝎射手魔羯”;
- vararr=[20,19,21,21,21,22,23,23,23,23,22,22];
- returns.substr(month*2-(day<arr[month-1]?2:0),2);
- }
- //取星座,参数分别是月份和日期
- functiongetxingzuo(month,day){
- //byGo_Rush(阿舜)fromhttp://ashun.cnblogs.com/
- vard=newDate(1999,month-1,day,0,0,0);
- vararr=[];
- arr.push(["魔羯座",newDate(1999,0,1,0,0,0)])
- arr.push(["水瓶座",newDate(1999,0,20,0,0,0)])
- arr.push(["双鱼座",newDate(1999,1,19,0,0,0)])
- arr.push(["牡羊座",newDate(1999,2,21,0,0,0)])
- arr.push(["金牛座",newDate(1999,3,21,0,0,0)])
- arr.push(["双子座",newDate(1999,4,21,0,0,0)])
- arr.push(["巨蟹座",newDate(1999,5,22,0,0,0)])
- arr.push(["狮子座",newDate(1999,6,23,0,0,0)])
- arr.push(["处女座",newDate(1999,7,23,0,0,0)])
- arr.push(["天秤座",newDate(1999,8,23,0,0,0)])
- arr.push(["天蝎座",newDate(1999,9,23,0,0,0)])
- arr.push(["射手座",newDate(1999,10,22,0,0,0)])
- arr.push(["魔羯座",newDate(1999,11,22,0,0,0)])
- for(vari=arr.length-1;i>=0;i–){
- if(d>=arr[i][1])returnarr[i][0];
- }
- }
- functiongetxingzuo(month,day){
- vars=”魔羯水瓶双鱼牡羊金牛双子巨蟹狮子处女天秤天蝎射手魔羯”;
- vararr=[19,50,84,116,148,181,214,246,278,310,341,373,383];
- for(vari=0;i<arr.length;i++){
- if((((month-1)<<5)+day)<=arr[i])returns.substr(i*2,2);
- }
- return“error”;
- }
- 计算生肖的:
- functionbirthday2BornTag($birthday){
- $year=substr($birthday,0,4);
- $bornTagarray=array(“猴”,“鸡”,“狗”,“猪”,“鼠”,“牛”,“虎”,“兔”,“龙”,“蛇”,
- “马”,“羊”);
- $index=$year%12;
- $bornTag=$bornTagarray[$index];
- return$bornTag;
- }
- echobirthday2BornTag(’1983-12-19′);
郑重声明:本文版权归原作者所有,转载文章仅为传播更多信息之目的,如作者信息标记有误,请第一时间联系我们修改或删除,多谢。
新闻热点
疑难解答