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实例代码如下:
<?php /** *getConstellation 根据出生生日取得星座 * *@param String $brithday 用于得到星座的日期 格式为yyyy-mm-dd * *@param Array $format 用于返回星座的名称 * *@return String */ function getConstellation($birthday, $format = null) { $pattern = '/^\d{4}-\d{1,2}-\d{1,2}$/'; if (!preg_match($pattern, $birthday, $matchs)) { return null; } $date = explode('-', $birthday); $year = $date[0]; $month = $date[1]; $day = $date[2]; if ($month < 1 || $month > 12 || $day < 1 || $day > 31) { return null; } //设定星座数组 $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) { $values = $format; } else { $values = $constellations; } //根据月份和日期判断星座 switch ($month) { case 1: if ($day <= $enddays[0]) { $constellation = $values[0]; } else { $constellation = $values[1]; } break; case 2: if ($day <= $enddays[1]) { $constellation = $values[1]; } else { $constellation = $values[2]; } break; case 3: if ($day <= $enddays[2]) { $constellation = $values[2]; } else { $constellation = $values[3]; } break; case 4: if ($day <= $enddays[3]) { $constellation = $values[3]; } else { $constellation = $values[4]; } break; case 5: if ($day <= $enddays[4]) { $constellation = $values[4]; } else { $constellation = $values[5]; } break; case 6: if ($day <= $enddays[5]) { $constellation = $values[5]; } else { $constellation = $values[6]; } break; case 7: if ($day <= $enddays[6]) { $constellation = $values[6]; } else { $constellation = $values[7]; } break; case 8: if ($day <= $enddays[7]) { $constellation = $values[7]; } else { $constellation = $values[8]; } break; case 9: if ($day <= $enddays[8]) { $constellation = $values[8]; } else { $constellation = $values[9]; } break; case 10: if ($day <= $enddays[9]) { $constellation = $values[9]; } else { $constellation = $values[10]; } break; case 11: if ($day <= $enddays[10]) { $constellation = $values[10]; } else { $constellation = $values[11]; } break; case 12: if ($day <= $enddays[11]) { $constellation = $values[11]; } else { $constellation = $values[0]; } break; } return $constellation; } ?>
js格式的:
// 根据生日的月份和日期,计算星座。 http://blog.111cn.net/cuixiping/ function getAstro(month, day) { var s = "魔羯水瓶双鱼牡羊金牛双子巨蟹狮子处女天秤天蝎射手魔羯"; var arr = [20, 19, 21, 21, 21, 22, 23, 23, 23, 23, 22, 22]; return s.substr(month * 2 - (day < arr[month - 1] ? 2 : 0), 2); } // 取星座, 参数分别是 月份和日期 function getxingzuo(month, day) { //by Go_Rush(阿舜) from http://ashun.cnblogs.com/ var d = new Date(1999, month - 1, day, 0, 0, 0); var arr = []; arr.push(["魔羯座", new Date(1999, 0, 1, 0, 0, 0)]) arr.push(["水瓶座", new Date(1999, 0, 20, 0, 0, 0)]) arr.push(["双鱼座", new Date(1999, 1, 19, 0, 0, 0)]) arr.push(["牡羊座", new Date(1999, 2, 21, 0, 0, 0)]) arr.push(["金牛座", new Date(1999, 3, 21, 0, 0, 0)]) arr.push(["双子座", new Date(1999, 4, 21, 0, 0, 0)]) arr.push(["巨蟹座", new Date(1999, 5, 22, 0, 0, 0)]) arr.push(["狮子座", new Date(1999, 6, 23, 0, 0, 0)]) arr.push(["处女座", new Date(1999, 7, 23, 0, 0, 0)]) arr.push(["天秤座", new Date(1999, 8, 23, 0, 0, 0)]) arr.push(["天蝎座", new Date(1999, 9, 23, 0, 0, 0)]) arr.push(["射手座", new Date(1999, 10, 22, 0, 0, 0)]) arr.push(["魔羯座", new Date(1999, 11, 22, 0, 0, 0)]) for (var i = arr.length - 1; i >= 0; i & ndash; ) { if (d >= arr[i][1]) return arr[i][0]; } } function getxingzuo(month, day) { var s = "魔羯水瓶双鱼牡羊金牛双子巨蟹狮子处女天秤天蝎射手魔羯"; var arr = [19, 50, 84, 116, 148, 181, 214, 246, 278, 310, 341, 373, 383]; for (var i = 0; i < arr.length; i++) { if ((((month - 1) << 5) + day) <= arr[i]) return s.substr(i * 2, 2); } return "error"; }
计算生肖的:
<?php function birthday2BornTag($birthday) { $year = substr($birthday, 0, 4); $bornTagarray = array("猴", "鸡", "狗", "猪", "鼠", "牛", "虎", "兔", "龙", "蛇", "马", "羊"); $index = $year % 12; $bornTag = $bornTagarray[$index]; return $bornTag; } echo birthday2BornTag('1983-12-19'); ?>
本文地址:http://www.phprm.com/riqi/fs4014.html
转载随意,但请附上文章地址:-)