Created
October 29, 2012 13:48
-
-
Save samma835/3973655 to your computer and use it in GitHub Desktop.
根据日期获取星座
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// $birthday的格式为yyyy-mm-dd | |
public 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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment