利用PHP怎么实现一个年龄计算函数

这期内容当中小编将会给大家带来有关利用PHP怎么实现一个年龄计算函数,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。

具体方法如下

<?php 
/** 
* PHP 年龄计算函数 
* 
* 参数支持数组传参和标准的 Mysql date 类型传参 
* params sample 
* -------------------------------------------------- 
$birthArr = array( 
'year' => '2000', 
'month' => '11', 
'day' => '3' 
); 
$birthStr = '2000-11-03'; 
* -------------------------------------------------- 
* ); 
* @author IT不倒翁 <itbudaoweng@gmail.com> 
* @copyright (c) 2011,2012 Just Use It! 
* @link IT不倒翁 http://yungbo.com 
* @param string|array $birthday 
* @return number $age 
*/ 
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 = substr($day, 0, 2); //get the first two chars in case of '2000-11-03 12:12:00' 
} 
} 
$age = date('Y') - $year; 
if (date('m') < $month || (date('m') == $month && date('d') < $day)) $age--; 
return $age; 
}

上述就是小编为大家分享的利用PHP怎么实现一个年龄计算函数了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注行业资讯频道。