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:php输入月份与日来计算星座的方法
function get_constellation($birth_month,$birth_date){ //判断的时候,为避免出现1和true的疑惑,或是判断语句始终为真的问题,这里统一处理成字符串形式 $birth_month = strval($birth_month); $constellation_name = array('水瓶座','双鱼座','白羊座','金牛座','双子座','巨蟹座','狮子座','处女座','天秤座','天蝎座)','射手座','摩羯座'); if ($birth_date <= 22){ if ('1' !== $birth_month){ $constellation = $constellation_name[$birth_month-2]; }else{ $constellation = $constellation_name[11]; } }else{ $constellation = $constellation_name[$birth_month-1]; } return $constellation; } echo get_constellation(8,11); echo get_constellation(9,11);
示例2:php 通过月分与日期来获取星座的方法
function get_xingzuo($month, $day){ $xingzuo = ''; // 检查参数有效性 if ($month < 1 || $month > 12 || $day < 1 || $day > 31){ return ''; } if(($month == 1 && $day >= 20) || ($month == 2 && $day <= 18)) { $xingzuo = "水瓶座"; }else if(($month == 2 && $day >= 19) || ($month == 3 && $day <= 20)) { $xingzuo = "双鱼座"; }else if (($month == 3 && $day >= 21) || ($month == 4 && $day <= 19)) { $xingzuo = "白羊座"; }else if (($month == 4 && $day >= 20) || ($month == 5 && $day <= 20)) { $xingzuo = "金牛座"; }else if (($month == 5 && $day >= 21) || ($month == 6 && $day <= 21)) { $xingzuo = "双子座"; }else if (($month == 6 && $day >= 22) || ($month == 7 && $day <= 22)) { $xingzuo = "巨蟹座"; }else if (($month == 7 && $day >= 23) || ($month == 8 && $day <= 22)) { $xingzuo = "狮子座"; }else if (($month == 8 && $day >= 23) || ($month == 9 && $day <= 22)) { $xingzuo = "处女座"; }else if (($month == 9 && $day >= 23) || ($month == 10 && $day <= 23)) { $xingzuo = "天秤座"; }else if (($month == 10 && $day >= 24) || ($month == 11 && $day <= 22)) { $xingzuo = "天蝎座"; }else if (($month == 11 && $day >= 23) || ($month == 12 && $day <= 21)) { $xingzuo = "射手座"; }else if (($month == 12 && $day >= 22) || ($month == 1 && $day <= 19)) { $xingzuo = "摩羯座"; } return $xingzuo; } echo get_xingzuo(8,11); echo get_xingzuo(9,11);
示例3:php获取星座的方法,需要输入月份与日期
function getConstellation($month, $day){ if(($month < 1 || $month > 12) || ($day < 1 || $day > 31)) return false; $constellations = [ ['20' => '水瓶座'], ['19' => '双鱼座'], ['21' => '白羊座'], ['20' => '金牛座'], ['21' => '双子座'], ['22' => '巨蟹座'], ['23' => '狮子座'], ['23' => '处女座'], ['23' => '天秤座'], ['24' => '天蝎座'], ['22' => '射手座'], ['22' => '魔羯座'] ]; list($constellation_key, $constellation_value) = each($constellations[(int)$month - 1]); if($day < $constellation_key){ list($constellation_key, $constellation_value) = each($constellations[($month - 2 < 0) ? $month = 11 : $month -= 2]); } return $constellation_value; } echo getConstellation(8,11); echo getConstellation(9,11);
示例4:php 通过格式化的日期输出星座
// 输入格式化后的日期,(年-月-日) function getConstellation($birthday){ $date = explode('-', $birthday); $month = $date[1]; $day = $date[2]; //英文星座数组 // $values = array('Capricorn','Aquarius','Pisces','Aries','Taurus','Gemini','Cancer','Leo','Virgo','Libra','Scorpio','Sagittarius'); //汉字星座数组 $values = array('摩羯座','水瓶座','双鱼座','白羊座','金牛座','双子座','巨蟹座','狮子座','处女座','天秤座','天蝎座','射手座'); //设定星座结束日期的数组,用于判断 $enddays = array(19, 18, 20, 20, 20, 21, 22, 22, 22, 22, 21, 21,); switch ($month){ case 1: return $day <= $enddays[0] ? $values[0] : $values[1]; break; case 2: return $day <= $enddays[1] ? $values[1] : $values[2]; break; case 3: return $day <= $enddays[2] ? $values[2] : $values[3]; break; case 4: return $day <= $enddays[3] ? $values[3] : $values[4]; break; case 5: return $day <= $enddays[4] ? $values[4] : $values[5]; break; case 6: return $day <= $enddays[5] ? $values[5] : $values[6]; break; case 7: return $day <= $enddays[6] ? $values[6] : $values[7]; break; case 8: return $day <= $enddays[7] ? $values[7] : $values[8]; break; case 9: return $day <= $enddays[8] ? $values[8] : $values[9]; break; case 10: return $day <= $enddays[9] ? $values[9] : $values[10]; break; case 11: return $day <= $enddays[10] ? $values[10] : $values[11]; break; case 12: return $day <= $enddays[11] ? $values[11] : $values[0]; break; } return ''; } echo getConstellation('1980-08-11'); echo getConstellation('1980-09-11');
本文php 通过日期推算星座的方法到此结束。人活着就是为了解决困难。这才是生命的意义,也是生命的资料。逃避不是办法,知难而上往往是解决问题的最好手段。小编再次感谢大家对我们的支持!