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

相关文章

fleaphp常用方法分页之Pager使用方法

Pager 分页函数 复制代码 代码如下: /** * 构造函数 * * 如果 $source 参数是一个 TableDataGateway 对象,则 FLEA_Helper_Pager...

php压缩和解压缩字符串的方法

本文实例讲述了php压缩和解压缩字符串的方法。分享给大家供大家参考。具体如下: 下面php代码通过gzcompress和gzuncompress压缩和解压缩字符串,可以设定压缩级别...

PHP 地址栏信息的获取代码

复制代码 代码如下:<?php //获取域名或主机地址 echo $_SERVER['HTTP_HOST']."<br>"; //获取网页地址 echo $_SERVE...

php冒泡排序与快速排序实例详解

本文实例分析了php冒泡排序与快速排序算法。分享给大家供大家参考,具体如下: $a=array('3','8','1','4','11','7'); print_r($a); $le...

php内核解析:PHP中的哈希表

PHP中使用最为频繁的数据类型非字符串和数组莫属,PHP比较容易上手也得益于非常灵活的数组类型。 在开始详细介绍这些数据类型之前有必要介绍一下哈希表(HashTable)。 哈希表是PH...