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

yipeiwu_com5年前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来实现网络服务

作者:samisa 以下文中的翻译名称对照表 : payload: 交谈内容 object: 实例 function: 函数 使用 php来实现网络服务 使用框架: WSO2 WSF/P...

php使用ZipArchive函数实现文件的压缩与解压缩

PHP ZipArchive 是PHP自带的扩展类,可以轻松实现ZIP文件的压缩和解压,使用前首先要确保PHP ZIP 扩展已经开启,具体开启方法这里就不说了,不同的平台开启PHP扩增的...

php输出文字乱码的解决方法

php输出文字乱码的解决办法: 在php文件最开头写上: <?php header('Content-type: text/html; charset=UTF8');...

PHP转换文件夹下所有文件编码的实现代码

PHP转换文件夹下所有文件的编码 适合发布网站的其他编码版本 比如你有一个GBK版本 你想有一个UTF8版本 或者你只有GBK的源码 你想二次开发 但是你不想改变IDE的编码方式 你可以...

php array_chunk()函数用法与注意事项

本文实例讲述了php array_chunk()函数用法与注意事项。分享给大家供大家参考,具体如下: 定义和用法 array_chunk() 函数把数组分割为新的数组块。 其中每个数组的...