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

相关文章

thinkphp 字母函数详解T/I/N/D/M/A/R/U

1、 /** * 获取模版文件 格式 资源://模块@主题/控制器/操作 * @param string $template 模版资源地址 * @param string $l...

PHP判断一个数组是另一个数组子集的方法详解

PHP判断一个数组是另一个数组子集的方法详解

本文实例讲述了PHP判断一个数组是另一个数组子集的方法。分享给大家供大家参考,具体如下: 前言 今天完成一个算法的过程中,有几个需求模块,其中就有判断$a数组是否是$b数组的子集,可能最...

php邮件发送的两种方式

这篇文章研究的主要内容就是使用PHP来发送电子邮件,总结为以下两种方法: 一、使用PHP内置的mail()函数 <?php $to = "test@163.com";...

一个显示某段时间内每个月的方法 返回由这些月份组成的数组

复制代码 代码如下: /** * 生成从开始月份到结束月份的月份数组 * 该方法仿照党子皓getDateArr()方法 * @param unknown_type $start * @p...

PHP直接修改表内容DataGrid功能实现代码

PHP直接修改表内容DataGrid功能实现代码

由于需要连接Oracle所以从二次开发和页面样式来说个人觉得phpMyDataGrid还是比较好上手。 1. 创建测试数据库和表 create database `guru`;...