PHP 年龄计算函数(精确到天)

yipeiwu_com6年前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;
}

相关文章

PHP5中的时间相差8小时的解决办法

方法1:        找到php.ini中的“;date.timezone =”这行,将“;”去掉,改成...

PHP面向对象学习笔记之一 基础概念

1> if( "false" ) 等效于 if( true), 因为非空字符串是true 2> 检查数据类型: is_array(); is_object(); is_str...

php5.2 Json不能正确处理中文、GB编码的解决方法

php5.2新增的json功能是非常受欢迎的,但是经过测试发现, json_encode对中文的处理是有问题的, 1.不能处理GB编码,所有的GB编码都会替换成空字符. 2.utf8编码...

LINUX下PHP程序实现WORD文件转化为PDF文件的方法

本文实例讲述了LINUX下PHP程序实现WORD文件转化为PDF文件的方法。分享给大家供大家参考,具体如下: <?php set_time_limit(0); func...

PHP中抽象类、接口的区别与选择分析

本文实例分析了PHP中抽象类、接口的区别与选择。分享给大家供大家参考,具体如下: 区别: 1、对接口的使用是通过关键字implements。对抽象类的使用是通过关键字extends。当然...