php 根据生日计算星座和生肖程序

一个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实例代码如下:

  1. /**
  2. *getConstellation 根据出生生日取得星座
  3. *
  4. *@param String $brithday 用于得到星座的日期 格式为yyyy-mm-dd
  5. *
  6. *@param Array $format 用于返回星座的名称
  7. *
  8. *@return String
  9. */
  10. function getConstellation($birthday, $format=null)
  11. {
  12. $pattern = ‘/^d{4}-d{1,2}-d{1,2}$/’;
  13. if (!preg_match($pattern, $birthday, $matchs))
  14. {
  15. return null;
  16. }
  17. $date = explode(‘-’, $birthday);
  18. $year = $date[0];
  19. $month = $date[1];
  20. $day = $date[2];
  21. if ($month <1 || $month>12 || $day < 1 || $day >31)
  22. {
  23. return null;
  24. }
  25. //设定星座数组
  26. $constellations = array(
  27. ‘摩羯座’, ‘水瓶座’, ‘双鱼座’, ‘白羊座’, ‘金牛座’, ‘双子座’,
  28. ‘巨蟹座’,'狮子座’, ‘处女座’, ‘天秤座’, ‘天蝎座’, ‘射手座’,);
  29. //或 ‍‍$constellations = array(
  30. ‘Capricorn’, ‘Aquarius’, ‘Pisces’, ‘Aries’, ‘Taurus’, ‘Gemini’,
  31. ‘Cancer’,'Leo’, ‘Virgo’, ‘Libra’, ‘Scorpio’, ‘Sagittarius’,);
  32. //设定星座结束日期的数组,用于判断
  33. $enddays = array(19, 18, 20, 20, 20, 21, 22, 22, 22, 22, 21, 21,);
  34. //如果参数format被设置,则返回值采用format提供的数组,否则使用默认的数组
  35. if ($format != null)
  36. {
  37. $values = $format;
  38. }
  39. else
  40. {
  41. $values = $constellations;
  42. }
  43. //根据月份和日期判断星座
  44. switch ($month)
  45. {
  46. case 1:
  47. if ($day <= $enddays[0])
  48. {
  49. $constellation = $values[0];
  50. }
  51. else
  52. {
  53. $constellation = $values[1];
  54. }
  55. break;
  56. case 2:
  57. if ($day <= $enddays[1])
  58. {
  59. $constellation = $values[1];
  60. }
  61. else
  62. {
  63. $constellation = $values[2];
  64. }
  65. break;
  66. case 3:
  67. if ($day <= $enddays[2])
  68. {
  69. $constellation = $values[2];
  70. }
  71. else
  72. {
  73. $constellation = $values[3];
  74. }
  75. break;
  76. case 4:
  77. if ($day <= $enddays[3])
  78. {
  79. $constellation = $values[3];
  80. }
  81. else
  82. {
  83. $constellation = $values[4];
  84. }
  85. break;
  86. case 5:
  87. if ($day <= $enddays[4])
  88. {
  89. $constellation = $values[4];
  90. }
  91. else
  92. {
  93. $constellation = $values[5];
  94. }
  95. break;
  96. case 6:
  97. if ($day <= $enddays[5])
  98. {
  99. $constellation = $values[5];
  100. }
  101. else
  102. {
  103. $constellation = $values[6];
  104. }
  105. break;
  106. case 7:
  107. if ($day <= $enddays[6])
  108. {
  109. $constellation = $values[6];
  110. }
  111. else
  112. {
  113. $constellation = $values[7];
  114. }
  115. break;
  116. case 8:
  117. if ($day <= $enddays[7])
  118. {
  119. $constellation = $values[7];
  120. }
  121. else
  122. {
  123. $constellation = $values[8];
  124. }
  125. break;
  126. case 9:
  127. if ($day <= $enddays[8])
  128. {
  129. $constellation = $values[8];
  130. }
  131. else
  132. {
  133. $constellation = $values[9];
  134. }
  135. break;
  136. case 10:
  137. if ($day <= $enddays[9])
  138. {
  139. $constellation = $values[9];
  140. }
  141. else
  142. {
  143. $constellation = $values[10];
  144. }
  145. break;
  146. case 11:
  147. if ($day <= $enddays[10])
  148. {
  149. $constellation = $values[10];
  150. }
  151. else
  152. {
  153. $constellation = $values[11];
  154. }
  155. break;
  156. case 12:
  157. if ($day <= $enddays[11])
  158. {
  159. $constellation = $values[11];
  160. }
  161. else
  162. {
  163. $constellation = $values[0];
  164. }
  165. break;
  166. }
  167. return $constellation;
  168. }
  169. js格式的:
  170. 根据生日的月份和日期,计算星座的js小函数(最简)
  171. // 根据生日的月份和日期,计算星座。 http://blog.111cn.net/cuixiping/
  172. function getAstro(month,day){
  173. var s=”魔羯水瓶双鱼牡羊金牛双子巨蟹狮子处女天秤天蝎射手魔羯”;
  174. var arr=[20,19,21,21,21,22,23,23,23,23,22,22];
  175. return s.substr(month*2-(day<arr[month-1]?2:0),2);
  176. }
  177. // 取星座, 参数分别是 月份和日期
  178. function getxingzuo(month,day){
  179. //by Go_Rush(阿舜) from http://ashun.cnblogs.com/
  180. var d=new Date(1999,month-1,day,0,0,0);
  181. var arr=[];
  182. arr.push(["魔羯座",new Date(1999, 0, 1,0,0,0)])
  183. arr.push(["水瓶座",new Date(1999, 0,20,0,0,0)])
  184. arr.push(["双鱼座",new Date(1999, 1,19,0,0,0)])
  185. arr.push(["牡羊座",new Date(1999, 2,21,0,0,0)])
  186. arr.push(["金牛座",new Date(1999, 3,21,0,0,0)])
  187. arr.push(["双子座",new Date(1999, 4,21,0,0,0)])
  188. arr.push(["巨蟹座",new Date(1999, 5,22,0,0,0)])
  189. arr.push(["狮子座",new Date(1999, 6,23,0,0,0)])
  190. arr.push(["处女座",new Date(1999, 7,23,0,0,0)])
  191. arr.push(["天秤座",new Date(1999, 8,23,0,0,0)])
  192. arr.push(["天蝎座",new Date(1999, 9,23,0,0,0)])
  193. arr.push(["射手座",new Date(1999,10,22,0,0,0)])
  194. arr.push(["魔羯座",new Date(1999,11,22,0,0,0)])
  195. for(var i=arr.length-1;i>=0;i–){
  196. if (d>=arr[i][1]) return arr[i][0];
  197. }
  198. }
  199. function getxingzuo(month,day){
  200. var s=”魔羯水瓶双鱼牡羊金牛双子巨蟹狮子处女天秤天蝎射手魔羯”;
  201. var arr=[19,50,84,116,148,181,214,246,278,310,341,373,383];
  202. for(var i=0;i<arr.length;i++){
  203. if ((((month-1)<<5)+day) <= arr[i]) return s.substr(i*2,2);
  204. }
  205. return “error”;
  206. }
  207. 计算生肖的:
  208. function birthday2BornTag($birthday){
  209. $year = substr($birthday,0,4);
  210. $bornTagarray = array(“猴”, “鸡”, “狗”, “猪”, “鼠”, “牛”, “虎”, “兔”, “龙”, “蛇”,
  211. “马”, “羊”);
  212. $index = $year%12;
  213. $bornTag = $bornTagarray[$index];
  214. return $bornTag;
  215. }
  216. echo birthday2BornTag(’1983-12-19′);