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

//计算年龄

 代码如下 复制代码
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(
            '鼠', '牛', '虎', '兔', '龙', '蛇', 
            '马', '羊', '猴', '鸡', '狗', '猪'
    );
    $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('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 < $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);      // 双子座

时间: 2024-08-22 14:46:42

php根据生日计算年龄/生肖/星座实例的相关文章

php根据出生日期计算年龄 生肖 星座程序

例1  代码如下 复制代码 function getAge($birthday) {  $age = 0;  $year = $month = $day = 0;  if (is_array($birthday)) {   extract($birthday);  } else {   if (strpos($birthday, '-') !== false) {    list($year, $month, $day) = explode('-', $birthday);    $day =

jQuery实现根据生日计算年龄 星座 生肖_jquery

<html> <head> <title></title> <script src="Scripts/jquery-1.4.1.js" type="text/javascript"></script> <script type="text/javascript"> //根据输入的生日自动获取星座,生肖和年龄. var year = new Array("

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

本文实例讲述了php根据生日计算年龄的方法.分享给大家供大家参考.具体如下: <?php function birthday($birthday){ $age = strtotime($birthday); if($age === false){ return false; } list($y1,$m1,$d1) = explode("-",date("Y-m-d",$age)); $now = strtotime("now"); list

根据生日计算年龄的JS代码

例  代码如下 复制代码 function displayage( yr, mon, day, countunit, decimals, rounding ) {     // Starter Variables     today = new Date();     yr = parseInt(yr);     mon = parseInt(mon);     day = parseInt(day);     var one_day = 1000*60*60*24;     var one_m

如何利用JS通过身份证号获取当事人的生日、年龄、性别_javascript技巧

身份证可以识别一个人的信息,下面就介绍一下如何利用js通过身份证号码获取当事人的年龄和性别. <script> function IdCard(UUserCard,num){ if(num==1){ //获取出生日期 birth=UUserCard.substring(6, 10) + "-" + UUserCard.substring(10, 12) + "-" + UUserCard.substring(12, 14); return birth;

JS根据生日算年龄的方法

  本文实例讲述了JS根据生日算年龄的方法.分享给大家供大家参考.具体实现方法如下: ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 function parseDate(str){ if(str.match(/^d{4}[-/s+]d{1,2}[-

php计算年龄精准到年月日_php技巧

本文实例讲述了php计算年龄精准到年月日的方法.分享给大家供大家参考.具体如下: <?php /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ class Age { /** * 计算年龄

php计算年龄精准到年月日的程序

这篇文章主要介绍了php计算年龄精准到年月日的方法,涉及php操作日期与字符串的相关技巧,非常简单实用,需要的朋友可以参考下 本文实例讲述了php计算年龄精准到年月日的方法.分享给大家供大家参考.具体如下:  $ni) {       $not_birth = 1;       $tmp = array($byear, $bmonth, $bday);       list($byear, $bmonth, $bday) = array($year, $month, $day);       l

php简单计算年龄的方法(周岁与虚岁)_php技巧

本文实例讲述了php简单计算年龄的方法.分享给大家供大家参考,具体如下: /** * $date是时间戳 * $type为1的时候是虚岁,2的时候是周岁 */ function getAgeByBirth($date,$type = 1){ $nowYear = date("Y",time()); $nowMonth = date("m",time()); $nowDay = date("d",time()); $birthYear = date