星座计算 php版 javascript版

2011-10-17 11:46  1448人阅读  评论 (0)

显示用户的星座,呵呵。之前还没有这样的需求,今天就写了一个PHP版和javascript版。分享下

星座时间对照表:

水瓶座: 1-21 => 2-19 双鱼座: 2-20 => 3-20 白羊座: 3-21 => 4-20 金牛座: 4-21 => 5-21 双子座: 5-22 => 6-21 巨蟹座: 6-22 => 7-22 狮子座: 7-23 => 8-22 处女座: 8-23 => 9-23 天秤座: 9-24 => 10-23 天羯座: 10-24 => 11-22 射手座: 11-23 => 12-21 魔羯座: 12-22 => 1-20

PHP版:

date_default_timezone_set('Asia/Shanghai');

    function xingzuo($shengri){
        $sr = date('md', $shengri);
        $names = array('魔羯座', '水瓶座', '双鱼座', '白羊座', '金牛座', '双子座', '巨蟹座',
            '狮子座', '处女座', '天秤座', '天羯座', '射手座', '魔羯座');
        $times = array(
            '101'=>'120', '121'=>'219', '220'=>'320', '321'=>'420', '421'=>'521', 
            '522'=>'621', '622'=>'722', '723'=>'822', '823'=>'923', '924'=>'1023', 
            '1024'=>'1122', '1123'=>'1221', '1222'=>'1231',
        );

        $i = 0;
        foreach ($times as $s => $e){
            if($sr >= $s && $sr <=$e){
                break;
            }
            $i++;
        }
        return $names[$i];
    }

    // 闰年
    $t = strtotime('1987-02-28');
    echo date('L', $t), '|', xingzuo($t);

    // 闰年
    $t = strtotime('1988-02-29');
    echo date('L', $t), '|', xingzuo($t);

javascript版:

// 传入时间戳
function xingzuo(shengri){
    var sr = (shengri.getMonth()+1)*100+shengri.getDate();
    var names = ['魔羯座', '水瓶座', '双鱼座', '白羊座', '金牛座', '双子座', '巨蟹座',
        '狮子座', '处女座', '天秤座', '天羯座', '射手座', '魔羯座'];
    var times = [
        '101','120', '121','219', '220','320', '321','420', '421','521', 
        '522','621', '622','722', '723','822', '823','923', '924','1023', 
        '1024','1122', '1123','1221', '1222','1231',
    ];

    var j=0;
    for (var i=0; i= times[i] && sr <=times[i+1]){
            break;
        }
    }
    return names[j];
}
// 直接传入时间对象
var d = new Date(1988, 2-1 ,29);
alert(xingzuo(d));