Home  >  Article  >  php教程  >  计算 星座 PHP

计算 星座 PHP

PHP中文网
PHP中文网Original
2016-05-25 17:03:302598browse

计算 星座 PHP

<?php
/**
 *  计算.星座
 *
 * @param int $month 月份
 * @param int $day 日期
 * @return str
 */
function get_constellation($month, $day){
    $signs = array(
            array(&#39;20&#39;=>&#39;宝瓶座&#39;), array(&#39;19&#39;=>&#39;双鱼座&#39;),
            array(&#39;21&#39;=>&#39;白羊座&#39;), array(&#39;20&#39;=>&#39;金牛座&#39;),
            array(&#39;21&#39;=>&#39;双子座&#39;), array(&#39;22&#39;=>&#39;巨蟹座&#39;),
            array(&#39;23&#39;=>&#39;狮子座&#39;), array(&#39;23&#39;=>&#39;处女座&#39;),
            array(&#39;23&#39;=>&#39;天秤座&#39;), array(&#39;24&#39;=>&#39;天蝎座&#39;),
            array(&#39;22&#39;=>&#39;射手座&#39;), array(&#39;22&#39;=>&#39;摩羯座&#39;)
    );
    $key = (int)$month - 1;
    list($startSign, $signName) = each($signs[$key]);
    if( $day < $startSign ){
        $key = $month - 2 < 0 ? $month = 11 : $month -= 2;
        list($startSign, $signName) = each($signs[$key]);
    }
    return $signName;
}

echo get_constellation(12, 11);    // 射手座
echo get_constellation(6, 6);      // 双子座

2. [PHP]代码 

<?php
/**
 *  计算.星座
 * 
 * @param int $month 月份
 * @param int $date 年份
 * @return str
 */
function get_constellation($month, $date){
    $constellations = array(
            &#39;水瓶座&#39;, &#39;双鱼座&#39;, &#39;白羊座&#39;, &#39;金牛座&#39;, &#39;双子座&#39;, &#39;巨蟹座&#39;, 
            &#39;狮子座&#39;, &#39;处女座&#39;, &#39;天秤座&#39;, &#39;天蝎座&#39;, &#39;射手座&#39;, &#39;摩羯座&#39;
    );
    if( $date <= 22 ){
        if( 1 != $month ){
            $constellation = $constellations[$month - 2];
        }else{
            $constellation = $constellations[11];
        }
    }else{
        $constellation = $constellations[$month - 1];
    }
    return $constellation;
}

echo get_constellation(12, 11);    // 射手座
echo get_constellation(6, 6);      // 双子座

 以上就是计算 星座 PHP的内容,更多相关内容请关注PHP中文网(www.php.cn)!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn