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. linux内核剖析(六)Linux系统调用详解(实现机制分析)

    本文介绍了系统调用的一些实现细节.首先分析了系统调用的意义,它们与库函数和应用程序接口(API)有怎样的关系.然后,我们考察了Linux内核如何实现系统调用,以及执行系统调用的连锁反应:陷入内核,传递 ...

  2. puppeteer新手遇到的坑

    puppeteer安装以及遇到的坑 1. 环境和安装 Puppeteer 至少需要 Node v6.4.0,如要使用 async / await,只有 Node v7.6.0 或更高版本才支持. no ...

  3. 【OpenFOAM案例】01 elbow

    本案例演示利用OpenFOAM的icoFoam求解器计算弯曲管道中的混合流动问题. 1 拷贝tutorials文件 启动终端,且拷贝tutorials文件夹中的文件.利用命令: cp -r $FOAM ...

  4. python3 log 日志记录

    在调试的过程中,很多地方需要用到日志 如下 import logging LOG_FORMAT = "%(asctime)s - %(levelname)s - %(message)s&qu ...

  5. Spring-Cloud-Gateway 从升级到放弃

    1 为什么要升级为spring-cloud-gateway? Spring Cloud Gateway features: Built on Spring Framework 5, Project R ...

  6. 优化实现Mobile/Bumped Diffuse

    在上一篇帖子的基础上增加一张法线贴图即可: Shader "James/Scene/Bumped_Diffuse" { Properties { _MainTex ("B ...

  7. Oracle中查看所有表和字段

    获取表字段: select * from user_tab_columns where Table_Name='用户表' order by column_name 获取表注释: select * fr ...

  8. Laravel-mix 中文文档

    镜像地址 : https://segmentfault.com/a/1190000015049847原文地址: Laravel Mix Docs   概览   基本示例 larave-mix 是位于w ...

  9. Windows SDK DDK WDK (Windows Driver Kit) 区别

    首先,先从基础的东西说起,开发WINDOWS下的驱动程序,需要一个专门的开发包,如:开发JAVA程序,我们可能需要一个JDK,开发WINDOWS应用程序,我们需要WINDOWS的SDK,现在开发WIN ...

  10. 树莓派mariadb 设置密码

    参考: sudo mysql -u root -p select Host,User,plugin from mysql.user where User='root'; plugin(加密方式)是un ...