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;
}

相关文章

PHP简单操作MongoDB的方法(安装及增删改查)

PHP简单操作MongoDB的方法(安装及增删改查)

本文实例讲述了PHP简单操作MongoDB的方法。分享给大家供大家参考,具体如下: php操作MongoDB的话首先从网上下载MongoDB的扩展包,https://github.com...

php中flush()、ob_flush()、ob_end_flush()的区别介绍

flush()、ob_flush()、ob_end_flush()三者的区别:首先,说下buffer,它是一个内存地址空间,为4096(1kb)【在php.ini配置文件中找到outpu...

PHP实现微信退款的方法示例

本文实例讲述了PHP实现微信退款的方法。分享给大家供大家参考,具体如下:$obj = new WXRefund('参数'); $obj-&g...

深入PHP内存相关的功能特性详解

可能有的读者碰到过类似下面的错误吧:Fatal error: Allowed memory size of X bytes exhausted (tried to allocate Y...

php表单提交问题的解决方法

在此记录一下,以后不能在同一个地方摔倒了! 数据库为bbs,表为test.三个字段,分别为id,name,sex。id为auto_increment。 连接数据库的php文件conn.p...