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. 优麒麟 16.04 LTS(长期支持)版本

    Ubuntu Kylin (中文又被称为优麒麟)是基于Ubuntu的一款官方衍生版. 它是一款专门为中国市场打造的免费操作系统.它包括Ubuntu用户期待的各种功能,并配有必备的中文软件及程序. ht ...

  2. 最新的Delphi版本号对照

    The CompilerVersion constant identifies the internal version number of the Delphi compiler. It is de ...

  3. “2014年CityEngine三维建模与设计精英培训班”——全国巡回举办

    watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvYXJjZ2lzX2FsbA==/font/5a6L5L2T/fontsize/400/fill/I0JBQk ...

  4. 【转载】Linux下查看CPU、内存占用率

    不错的文章(linux系统性能监控--CPU利用率):https://blog.csdn.net/ctthuangcheng/article/details/52795477 在linux的系统维护中 ...

  5. AI习惯的数学书籍、计算机经典书籍

    http://download.csdn.net/download/wz619899442/8405297 https://www.amazon.com/Introduction-Automata-T ...

  6. atom汉化

    Atom 是 Github 专门为程序员推出的一个跨平台文本编辑器. 推荐一下 Atom官方网站https://atom.io/ GitHub 以后肯定会通过官方模块把 Atom 和 GitHub 进 ...

  7. springboot2.x接口返回中文乱码

    @Configuration public class GlobalConversConfiguration extends WebMvcConfigurationSupport { @Bean pu ...

  8. 此请求已被阻止,因为当用在 GET 请求中时,会将敏感信息透漏给第三方网站。若要允许 GET 请求,请将 JsonRequestBehavior 设置为 AllowGet。

    1.问题描述 mvc从一个路径获取所有的图片信息,ajax方法如下: function getimages(day) { var year = $("#selYear").val( ...

  9. 【gulp】gulp-file-include 合并 html 文件

    gulp-file-include 是 gulp 插件,它提供了一个 include 方法让我们可以像后端模板那样把公共部分的页面导入进来. 安装依赖包(包括了 gulp-file-include 和 ...

  10. ant 通配符

    ant 通配符 我们常用的匹配模式有ANT模式,比如acegi可以用PATTERN_TYPE_APACHE_ANT来使用ANT匹配模式,那什么是ANT匹配模式呢.   ANT通配符有三种:     通 ...