PHP日期得到星座

php相关:用日期得到相关的星座
/**
 * 星座判断
 */
function getConstellation($birthday, $format = null) {  
    @header ( Content-type: text/html;charset=UTF-8 );
    $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 (10-摩羯座, 11-水瓶座, 12-双鱼座, 1-白羊座, 2-金牛座, 3-双子座, 4-巨蟹座, 5-狮子座, 6-处女座, 7-天秤座, 8-天蝎座, 9-射手座 );

    //设定星座结束日期的数组,用于判断
    $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;
}
echo getConstellation ( 1990-07-01 );  

郝先生

继续阅读此作者的更多文章