php根据日期判断星座
<?php
header('Content-type: text/html;charset=UTF-8');
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(
'摩羯座', '水瓶座', '双鱼座', '白羊座', '金牛座', '双子座',
'巨蟹座','狮子座', '处女座', '天秤座', '天蝎座', '射手座',);
//设定星座结束日期的数组,用于判断
$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;
}
function get_sign($month,$day){
$month = intval ($month);
$day = intval ($day);
$signs = array(
array( "22" => "摩羯座"),
array( "20" => "宝瓶座"),
array( "19" => "双鱼座"),
array( "21" => "白羊座"),
array( "20" => "金牛座"),
array( "21" => "双子座"),
array( "22" => "巨蟹座"),
array( "23" => "狮子座"),
array( "23" => "处女座"),
array( "23" => "天秤座"),
array( "24" => "天蝎座"),
array( "22" => "射手座"),
);
if ( $month > 0 && $month < 13 && $day > 0 && $day < 32){
if ( $day >= key( $signs[$month % 12])) {
echo "he is ".$signs[$month % 12 ][key( $signs[$month % 12])];
} else {
echo "he is ".$signs[$month - 1][key($signs[$month-1])];
}
} else {
echo "wrong data";
}
}
get_sign(1,1);