根据生日获取年龄
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了根据生日获取年龄相关的知识,希望对你有一定的参考价值。
原文地址: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; }
以上是关于根据生日获取年龄的主要内容,如果未能解决你的问题,请参考以下文章