Heim  >  Artikel  >  Backend-Entwicklung  >  php根据生日计算年龄/生肖/星座实例

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

PHP中文网
PHP中文网Original
2017-03-22 16:53:222573Durchsuche

本文章来介绍根据用户出生年月来计算年龄/生肖/星座的各种程序实例代码,各位朋友不防进入参考

//计算年龄

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"; 
}

根据年份计算生肖

<?php 
/** 
 *  计算.生肖 
 *  
 * @param int $year 年份 
 * @return str 
 */ 
function get_animal($year){ 
    $animals = 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; 
    ); 
    $key = ($year - 1900) % 12; 
    return $animals[$key]; 
}
echo get_animal(1990);    // 马 
echo get_animal(2010);    // 虎

根据生日计算星座

<?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);      // 双子座

相关文章:

PHP根据生日计算年龄(周岁)

php根据生日计算年龄的方法

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn