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过滤html字符串,防止SQL注入的方法

批量过滤post,get敏感数据复制代码 代码如下:$_GET = stripslashes_array($_GET);$_POST = stripslashes_array($_POS...

PHP Zip解压 文件在线解压缩的函数代码

复制代码 代码如下: /********************** *@file - path to zip file *@destination - destination dire...

PHP工厂模式简单实现方法示例

本文实例讲述了PHP工厂模式简单实现方法。分享给大家供大家参考,具体如下: 工厂模式是一种类,建立了一个工厂来根据所需来创建对象,这种方式在多态性编程中是很重要的,允许动态替换类,修改配...

php allow_url_include的应用和解释

因为这个原因,许多安全研究人员建议在php.ini配置中禁用指向allow_url_fopen。不幸的是,许多推荐这种方法的人,并没有意识到,这样会破坏很多的应用并且并不能保证100%的...

PHP进制转换实例分析(2,8,16,36,64进制至10进制相互转换)

本文实例讲述了PHP进制转换。分享给大家供大家参考,具体如下: 可以实现: 10进制转换2、8、16、36、62进制 2、8、16、36、62进制转换10进制 有点要注意下,2、8、1...