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实现的美国50个州选择列表实例

本文实例讲述了php实现的美国50个州选择列表。分享给大家供大家参考。具体如下: 这里展示的是php生成的美国50个州的选择列表,自动选择当前州 <select name="s...

PHP n个不重复的随机数生成代码

复制代码 代码如下:<?php //range 是将1到100 列成一个数组 $numbers = range (1,100); //shuffle 将数组顺序随即打乱 shuff...

PHP数组函数array_multisort()用法实例分析

本文实例分析了PHP数组函数array_multisort()用法。分享给大家供大家参考,具体如下: 有时候我们需要对二维数组的某个键的值进行排序,这里就是讨论这个问题。我们可以使用ar...

PHP中cookies使用指南

综述    Cookie是在HTTP协议下,服务器或脚本可以维护客户工作站上信息的一种方式。Cookie是由Web服务器保存在用户浏览器上的小文件,它可以包含有关用户的信息(如...

解析php中static,const与define的使用区别

define部分:宏不仅可以用来代替常数值,还可以用来代替表达式,甚至是代码段。(宏的功能很强大,但也容易出错,所以其利弊大小颇有争议。)宏的语法为:#define 宏名称 宏值作为一种...