php几种根据生日计算年龄的方法
2016-09-29 14:05
513 查看
方法一:
/** * @uses 根据生日计算年龄,年龄的格式是:2016-09-23 * @param string $birthday * @return string|number */ public function calcAge($birthday) { $iage = 0; if (!empty($birthday)) { $year = date('Y',strtotime($birthday)); $month = date('m',strtotime($birthday)); $day = date('d',strtotime($birthday)); $now_year = date('Y'); $now_month = date('m'); $now_day = date('d'); if ($now_year > $year) { $iage = $now_year - $year - 1; if ($now_month > $month) { $iage++; } else if ($now_month == $month) { if ($now_day >= $day) { $iage++; } } } } return $iage; }方法二:
public function calcAge($birthday) { $age = 0; if(!empty($birthday)){ $age = strtotime($birthday); if($age === false){ return 0; } list($y1,$m1,$d1) = explode("-",date("Y-m-d", $age)); list($y2,$m2,$d2) = explode("-",date("Y-m-d"), time()); $age = $y2 - $y1; if((int)($m2.$d2) < (int)($m1.$d1)){ $age -= 1; } } return $age; }
相关文章推荐
- 一个关于if else容易迷惑的问题
- PHP5.2.*防止Hash冲突拒绝服务攻击的Patch
- 深入理解PHP之匿名函数
- JSP/PHP基于Ajax的分页功能实现
- 关于PHP通过PDO用中文条件查询MySQL的问题。
- 什么是设计模式
- PHP数据库长连接mysql_pconnect的细节
- Php Installing An Expansion
- Laravel5.3 表单验证中文报错提示语言包
- 10 件在 PHP 7 中不要做的事情
- rem : web app适配的秘密武器
- jquery高级应用之Deferred对象
- 关于浮动与清除浮动,你应该知道的
- 数组reduce方法的高级技巧
- php7 读取php.ini[4]
- PHP+Apache在Windows 9x下的安装和配置
- IIS 6 的 PHP 最佳配置方法
- 安装Apache和PHP的一些补充