码迷,mamicode.com
首页 > 其他好文 > 详细

根据生日获取年龄

时间:2017-11-23 10:57:25      阅读:166      评论:0      收藏:0      [点我收藏+]

标签:nbsp   方法   lis   xpl   return   pre   ram   use   格式   

原文地址:http://www.jb51.net/article/69396.htm

 

1方法

/** 
     * @uses 根据生日计算年龄,年龄的格式是:2016-09-23 
     * @param string $birthday 
     * @return string|number 
     */  
    public function calcAge($birthday) {  
        $iage = 0;  
        if (!empty($birthday)) {  
            $year = date(‘Y‘,strtotime($birthday));  
            $month = date(‘m‘,strtotime($birthday));  
            $day = date(‘d‘,strtotime($birthday));  
              
            $now_year = date(‘Y‘);  
            $now_month = date(‘m‘);  
            $now_day = date(‘d‘);  
      
            if ($now_year > $year) {  
                $iage = $now_year - $year - 1;  
                if ($now_month > $month) {  
                    $iage++;  
                } else if ($now_month == $month) {  
                    if ($now_day >= $day) {  
                        $iage++;  
                    }  
                }  
            }  
        }  
        return $iage;  
    }  

 

2方法

public function calcAge($birthday) {  
        $age = 0;  
        if(!empty($birthday)){  
            $age = strtotime($birthday);  
            if($age === false){  
                return 0;  
            }  
              
            list($y1,$m1,$d1) = explode("-",date("Y-m-d", $age));  
              
            list($y2,$m2,$d2) = explode("-",date("Y-m-d"), time());  
              
            $age = $y2 - $y1;  
            if((int)($m2.$d2) < (int)($m1.$d1)){  
                $age -= 1;  
            }  
        }  
        return $age;  
    }  

 

根据生日获取年龄

标签:nbsp   方法   lis   xpl   return   pre   ram   use   格式   

原文地址:http://www.cnblogs.com/88phper/p/7883324.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!