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

相关文章

一道求$b相对于$a的相对路径的php代码

php面试题的题目: $a = '/a/b/c/d/e.php'; $b = '/a/b/12/34/c.php'; //计算出 $b 相对于 $a 的相对路径应该是 ../../c/d...

php中大括号作用介绍

一、不管什么程序,function name(){}, for(){}, ….太多了,不说也知道做什么用了。 二、$str{4}在字符串的变量的后面跟上{}刚大括号和中括号一样都是把某个...

使用php判断浏览器的类型和语言的函数代码

我们经常看到有一些网站上面会显示出你目前使用的浏览器类型和使用的语言,比如显示的是:您使用的浏览器为 IE6,繁体字。看起来是不是很炫。 其实这样的功能不难实现,无非就是判断浏览器的类型...

解析php二分法查找数组是否包含某一元素

二分法查找数组是否包含某一元素,兼容正反序,代码实现:复制代码 代码如下:<?php $searchValue = (int)$_GET['key']; fun...

PHP调用Linux命令权限不足问题解决方法

业务背景:  yourcmd为我的linux程序,它对权限要求非常严格,当用php去执行yourcmd程序 系统:CentOS 6.3 apache是php的执行用户 用exe...