/** *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格式的: 根據生日的月份和日期,計算星座的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–){ 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”; } 計算生肖的: function birthday2BornTag($birthday){ $year = substr($birthday,0,4); $bornTagarray = array(“猴”, “雞”, “狗”, “豬”, “鼠”, “牛”, “虎”, “兔”, “龍”, “蛇”, “馬”, “羊”); $index = $year%12; $bornTag = $bornTagarray[$index]; return $bornTag; } echo birthday2BornTag(’1983-12-19′); |