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获取url汉字出现乱码的解决方法

本文实例讲述了PHP采用get获取url汉字出现乱码的解决方法。分享给大家供大家参考。具体方法如下: 一、问题: 本来打算这样使用 复制代码 代码如下:<a href="list....

PHP使用in_array函数检查数组中是否存在某个值

本文实例讲述了PHP使用in_array函数检查数组中是否存在某个值的方法。分享给大家供大家参考。具体分析如下: PHP使用in_array()函数检查数组中是否存在某个值,如果存在则返...

php使用curl伪造浏览器访问操作示例

本文实例讲述了php使用curl伪造浏览器访问操作。分享给大家供大家参考,具体如下: 原理 服务器主要通过User-Agent识别客户端是何种设备 User-Agent是Http协议中的...

提高PHP编程效率的方法

1、如果能将类的方法定义成static,就尽量定义成static,它的速度会提升将近4倍。2、$row['id'] 的速度是$row[id]的7倍。3、echo 比 print 快,并且...

php 数组的合并、拆分、区别取值函数集

合并数组有三个函数: 1.array_combine() 携带两个参数数组,参数数组一的值作新数组的键,参数数组二的值作新数组的值。很简单。 例子: 复制代码 代码如下: <?ph...