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实现打包下载文件的方法示例

本文实例讲述了PHP实现打包下载文件的方法。分享给大家供大家参考,具体如下: /** * 下载文件 * @param $img * @return string */ public...

PHP下对字符串的递增运算代码

有同学问了一个问题: 复制代码 代码如下: <?php for($i = 'A'; $i <= 'Z'; $i++) { echo $i; } //输出是啥? 输出是: 复...

PHP实现移除数组中为空或为某值元素的方法

本文实例讲述了PHP实现移除数组中为空或为某值元素的方法。分享给大家供大家参考,具体如下: 在实现移除数组中项目为空的元素或为某值的元素时用到了两个函数 array_filter、cre...

kindeditor 加入七牛云上传的实例讲解

七牛云上传主要有两种: 服务端上传 前端上传,前端又分两种返回方式: 1).重定向返回,可以解决ajax跨域的问题 2).回调返回,七牛云先向服务端要返回数据,再由七牛云返回前端,解决不...

php实现扫描二维码根据浏览器类型访问不同下载地址

<?php $Agent = $_SERVER['HTTP_USER_AGENT']; preg_match('/android|iphone/i',$Agent,$m...