31,
2 => 28,
3 => 31,
4 => 30,
5 => 31,
6 => 30,
7 => 31,
8 => 31,
9 => 30,
10 => 31,
11 => 30,
12 => 31
);
$y = $m = $d = 0;
//天相减为正
if ($time['mday'] >= $birthday['mday']) {
//月相减为正
if ($time['mon'] >= $birthday['mon']) {
$y = $time['year'] - $birthday['year'];
$m = $time['mon'] - $birthday['mon'];
} else {
//月相减为负,借年
$y = $time['year'] - $birthday['year'] - 1;
$m = $time['mon'] - $birthday['mon'] + 12;
}
$d = $time['mday'] - $birthday['mday'];
return ($y == 0 ? '' : $y . '岁') . ($m == 0 ? '' : $m . '个月') . ($d == 0 || $y > 0 ? '' : $d . '天');
}
//天相减为负,借月
if ($time['mon'] == 1) { //1月,借年
$y = $time['year'] - $birthday['year'] - 1;
$m = $time['mon'] - $birthday['mon'] + 12;
$d = $time['mday'] - $birthday['mday'] + $monthDays[12];
return ($y == 0 ? '' : $y . '岁') . ($m == 0 ? '' : $m . '个月') . ($d == 0 || $y > 0 ? '' : $d . '天');
}
//3月,判断闰年取得2月天数
if ($time['mon'] == 3) {
$d = $time['mday'] - $birthday['mday'] + ($time['year'] % 4 == 0 ? 29 : 28);
} else {
$d = $time['mday'] - $birthday['mday'] + $monthDays[$time['mon'] - 1];
}
//借月后,月相减为正
if ($time['mon'] >= $birthday['mon'] + 1) {
$y = $time['year'] - $birthday['year'];
$m = $time['mon'] - $birthday['mon'] - 1;
} else {
//借月后,月相减为负,借年
$y = $time['year'] - $birthday['year'] - 1;
$m = $time['mon'] - $birthday['mon'] + 12 - 1;
}
return ($y == 0 ? '' : $y . '岁') . ($m == 0 ? '' : $m . '个月') . ($d == 0 || $y > 0 ? '' : $d . '天');
}
var_dump(datediffage($birthday, $time));

PHP计算年龄的更多相关文章

  1. iOS 获取当前时间以及计算年龄(时间差)

    获取当前时间 NSDate *now = [NSDate date]; NSLog(@"now date is: %@", now); NSCalendar *calendar = ...

  2. Java 计算年龄

    public static String getAgeTxt(String birthTime,String beginTime,int level){ if(StringUtils.isBlank( ...

  3. Excel中最精确的计算年龄的公式

    身份证算年龄 假设A1是身份证号所在单元格 =IF(MONTH(NOW())<INT(MID(A1,11,2)),INT(YEAR(NOW())-INT(MID(A1,7,4)))-1,IF(M ...

  4. sql 身份证计算年龄和性别

    IdentityNumber 是身份证号 年龄: ,), GETDATE()) / 365.25) as '推荐人年龄', 15位的身份证计算年龄: case when b.IdentityNumbe ...

  5. Java 根据出生日期计算年龄

    1.把出生日期字符串转换为日期格式. public static Date parse(String strDate) throws ParseException { SimpleDateFormat ...

  6. 在Excel中根据某一个单元格的出生日期自动精确计算年龄

    =IF(MONTH(NOW())<MONTH(G4),INT(YEAR(NOW())-YEAR(G4))-1,IF(MONTH(NOW())>MONTH(G4),YEAR(NOW())-Y ...

  7. SQL 根据日期精确计算年龄

    SQL 根据日期精确计算年龄 第一种: 一张人员信息表里有一人生日(Birthday)列,跟据这个列,算出该人员的年龄 datediff(year,birthday,getdate()) 例:birt ...

  8. [JavaScript]YYYY-MM-DD格式字符串计算年龄

    function getAge(birth){ birth = birth.replace(/-/g,"/"); //把格式中的"-"替换为"/&qu ...

  9. 依据出生日期Date 计算年龄

    依据出生日期计算年龄 public class DateGetAge { public static int getAge(Date birthDay) throws Exception { Cale ...

  10. 问题:oracle 计算年龄;结果:oracle中根据生日计算年龄的问题

    SELECT FLOOR(MONTHS_BETWEEN(SYSDATE,birthday)/12,1) FROM ltteacherinfo where name='朱雪东111'这个报错ORA 00 ...

随机推荐

  1. 转载:MVC升级以后出现"当前上下文中不存在ViewBag"的问题解决

    MVC升级以后出现"当前上下文中不存在ViewBag"的问题解决 把自己的项目从MVC4升级到了MVC5,结果问题一大堆,View的设计环境出现了"当前上下文中不存在Vi ...

  2. 删除 nuget 文件夹内容

    vs2017 ->工具->选项->NuGet 包管理器->清除所有NuGet缓存

  3. LaTeX中的各种距离设置总结

    LaTeX中的各种距离设置总结   1. 页面设置 A4 会给你一个较小的页面,为了使用更多的控制,可用 geometry宏包  和  命令 \layout . 2. 改变长度 在latex里改变长度 ...

  4. csproj文件中copy指令的使用方式

    实际开发中有很多项目需要引用第三方的dll或者资源文件,且文件比较多,在运行时这些文件需要被拷贝到BIN目录. 使用VS自带的"复制到输出目录",似然方便,但是比较不零活,经过多次 ...

  5. TypeScript和JavaScript的一些小技巧记录

    项目里使用到的技巧,记录一下,会持续更新. JS的技巧完全可以使用到TS上哦. JS 向下取整 Math.floor(4.5); 简写: var num = 4.5; ~~num; num <& ...

  6. 【转载】vi/vim使用进阶: 指随意动,移动如飞 (二)

    本节所用命令的帮助入口: :help usr_03.txt :help motion.txt :help usr_29.txt :help scroll.txt :help folding 上一篇文章 ...

  7. 【转】使用minizip解压缩多个文件(基于zlib)

    原帖子:使用minizip解压缩多个文件(基于zlib) 写作目的:之前在网上看到很多人在寻找可以解压缩多个文件的程序,其中有尝试zlib的,使用zlib的源码可以生成后缀为点gz的压缩文件,但是一次 ...

  8. Elasticsearch 性能监控基础【转】

    https://blog.csdn.net/yangwenbo214/article/details/74000458

  9. Spring钩子方法和钩子接口的使用详解

    本文转自:http://www.sohu.com/a/166804449_714863 前言 SpringFramework其实具有很高的扩展性,只是很少人喜欢挖掘那些扩展点,而且官方的Refrenc ...

  10. mysql 批量导入 Packets larger than max_allowed_packet are not allowed

    解决方法  :http://blog.csdn.net/gtosky4u/article/details/8581281