Home > php教程 > php手册 > php根据生日计算年龄/生肖/星座实例

php根据生日计算年龄/生肖/星座实例

WBOY
Release: 2016-05-25 16:54:40
Original
2518 people have browsed it
本文章来介绍根据用户出生年月来计算年龄/生肖/星座的各种程序实例代码,各位朋友不防进入参考


//计算年龄

 代码如下 复制代码
function birthday($mydate){
    $birth=$mydate;
    list($by,$bm,$bd)=explode('-',$birth);
    $cm=date('n');
    $cd=date('j');
    $age=date('Y')-$by-1;
    if ($cm>$bm || $cm==$bm && $cd>$bd) $age++;
    return $age;
//echo "生日:$birthn年龄:$agen";
}

根据年份计算生肖

 代码如下 复制代码

/**
 *  计算.生肖
 * 
 * @param int $year 年份
 * @return str
 */
function get_animal($year){
    $animals = array(
            '鼠', '牛', '虎', '兔', '龙', '蛇', 
            '马', '羊', '猴', '鸡', '狗', '猪'
    );
    $key = ($year - 1900) % 12;
    return $animals[$key];
}

echo get_animal(1990);    // 马
echo get_animal(2010);    // 虎

根据生日计算星座

 代码如下 复制代码


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

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



教程链接:

随意转载~但请保留教程地址★

Related labels:
source:php.cn
Statement of this Website
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
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template