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中$_GET与$_POST过滤sql注入的方法

本文实例讲述了php中$_GET与$_POST过滤sql注入的方法,分享给大家供大家参考。具体分析如下: 此函数只能过滤一些敏感的sql命令了,像id=1这种大家还是需要自己简单过滤了。...

PHP如何编写易读的代码

成功的开发团队要求队伍中的每一位成员遵守代码重用规则,这些规定把代码的重用性推到极至同时却不会显著降低开发人员的创造力和开发效率。如果编写和使用代码的开发人员遵守共同的程序命名规范代码和...

php中字符集转换iconv函数使用总结

iconv函数库能够完成各种字符集间的转换,是php编程中不可缺少的基础函数库。 用法如下: 复制代码 代码如下: $string = "欢迎访问【宜配屋www.yipeiwu.com】...

PHP聚合式迭代器接口IteratorAggregate用法分析

本文实例讲述了PHP聚合式迭代器接口IteratorAggregate用法。分享给大家供大家参考,具体如下: PHP IteratorAggregate又叫聚合式迭代器,它提供了创建外部...

php中get_meta_tags()、CURL与user-agent用法分析

本文实例分析了php中get_meta_tags()、CURL与user-agent用法。分享给大家供大家参考。具体分析如下: get_meta_tags()函数用于抓取网页中<m...