2011-03-02

[PHP] 取得生日對應的星座名稱

在 Google 上找了一下能用 function,花了十分鐘決定自己寫了,說實在的在 social 的網站上這算是基本需求,而以前寫的也不知道丟哪去了,這次好好記錄下來免得以後又要再寫一份。

function getStarSignsName($month, $day) {
    $list=array(
        array('name'=>"摩羯座",'min'=>'12-22','max'=>'01-19'),  
        array('name'=>"水瓶座",'min'=>'01-20','max'=>'02-18'),
        array('name'=>"雙魚座",'min'=>'02-19','max'=>'03-20'),
        array('name'=>"牡羊座",'min'=>'03-21','max'=>'04-19'),
        array('name'=>"金牛座",'min'=>'04-20','max'=>'05-20'),
        array('name'=>"雙子座",'min'=>'05-21','max'=>'06-21'),
        array('name'=>"巨蟹座",'min'=>'06-22','max'=>'07-22'),
        array('name'=>"獅子座",'min'=>'07-23','max'=>'08-22'),
        array('name'=>"處女座",'min'=>'08-23','max'=>'09-22'),
        array('name'=>"天秤座",'min'=>'09-23','max'=>'10-23'),
        array('name'=>"天蠍座",'min'=>'10-24','max'=>'11-22'),
        array('name'=>"射手座",'min'=>'11-23','max'=>'12-21'),
    ); 
    
    $time=strtotime("1970-$month-$day");
    foreach ($list as $row){
        $min=strtotime("1970-".$row['min']);
        $max=strtotime("1970-".$row['max']);
        if($min<=$time && $time<=$max){
            return $row['name'];
        }
    }
    /*other to 摩羯座*/
    return $list[0]['name'];
}


測試程式:
$test=array(
    array('n'=>"水瓶座",'m'=>'01','d'=>'20'),
    array('n'=>"水瓶座",'m'=>'02','d'=>'08'),
    array('n'=>"水瓶座",'m'=>'02','d'=>'18'),
    
    array('n'=>"雙魚座",'m'=>'02','d'=>'19'),
    array('n'=>"雙魚座",'m'=>'03','d'=>'10'),
    array('n'=>"雙魚座",'m'=>'03','d'=>'20'),
    
    array('n'=>"牡羊座",'m'=>'03','d'=>'21'),
    array('n'=>"牡羊座",'m'=>'04','d'=>'09'),
    array('n'=>"牡羊座",'m'=>'04','d'=>'19'),
    
    array('n'=>"金牛座",'m'=>'04','d'=>'20'),
    array('n'=>"金牛座",'m'=>'05','d'=>'10'),
    array('n'=>"金牛座",'m'=>'05','d'=>'20'),
    
    array('n'=>"雙子座",'m'=>'05','d'=>'21'),
    array('n'=>"雙子座",'m'=>'06','d'=>'1'),
    array('n'=>"雙子座",'m'=>'06','d'=>'21'),
    
    array('n'=>"巨蟹座",'m'=>'06','d'=>'22'),
    array('n'=>"巨蟹座",'m'=>'07','d'=>'02'),
    array('n'=>"巨蟹座",'m'=>'07','d'=>'22'),
    
    array('n'=>"獅子座",'m'=>'07','d'=>'23'),
    array('n'=>"獅子座",'m'=>'08','d'=>'02'),
    array('n'=>"獅子座",'m'=>'08','d'=>'22'),
    
    array('n'=>"處女座",'m'=>'08','d'=>'23'),
    array('n'=>"處女座",'m'=>'09','d'=>'02'),
    array('n'=>"處女座",'m'=>'09','d'=>'22'),
    
    array('n'=>"天秤座",'m'=>'09','d'=>'23'),
    array('n'=>"天秤座",'m'=>'10','d'=>'03'),
    array('n'=>"天秤座",'m'=>'10','d'=>'23'),
    
    array('n'=>"天蠍座",'m'=>'10','d'=>'24'),
    array('n'=>"天蠍座",'m'=>'11','d'=>'02'),
    array('n'=>"天蠍座",'m'=>'11','d'=>'22'),
    
    array('n'=>"射手座",'m'=>'11','d'=>'23'),
    array('n'=>"射手座",'m'=>'12','d'=>'02'),
    array('n'=>"射手座",'m'=>'12','d'=>'21'),
    
    array('n'=>"摩羯座",'m'=>'12','d'=>'22'),
    array('n'=>"摩羯座",'m'=>'01','d'=>'09'),
    array('n'=>"摩羯座",'m'=>'01','d'=>'19'),
);

foreach ($test as $v){
    if(getStarSignsName($v['m'],$v['d'])!==$v['n']){
        echo $v['n'],"error<br/>";
    }else{
        echo $v['n'],"OK<br/>";
    }
}

0 回應: