dongzhi2332 2015-08-17 23:50
浏览 39
已采纳

PHP或Javascript / Jquery - 计算新生儿/成人的年龄

All functions that I've seen get the difference of years from birthdate, and I want to get the age in months or even days.

Sample in php:

$date = "2015-05-23";
/* whatever */
echo "Has 2 months and 8 days";

$date = "2012-10-30";
/* whatever */
echo "Has 2 years and 2 months";

Nevermind if it's in javascript/jquery or php.

Thanks people!

  • 写回答

3条回答 默认 最新

  • donglao9606 2015-08-20 00:05
    关注

    I modified some code to get what I wanted. This work perfectly, with all the exceptions (if some year/month/day is 0, not show).

    I share with you my code in PHP, if someone need it too.

    echo getAge("10-02-2014");
    
    function getAge($fecha) {
    /* $fecha => d-m-Y (in this format!!) */
    $dob = strtotime($fecha);
    $current_time = time();
    $age_years = date('Y',$current_time) - date('Y',$dob);
    $age_months = date('m',$current_time) - date('m',$dob);
    $age_days = date('d',$current_time) - date('d',$dob);
    
    if ($age_days<0) {
        $days_in_month = date('t',$current_time);
        $age_months--;
        $age_days= $days_in_month+$age_days;
    }
    if ($age_months<0) {
        $age_years--;
        $age_months = 12+$age_months;
    }
    $todayString = date('d',$current_time).'-'.date('m',$current_time).'-'.date('Y',$current_time);
    /* keep in mind $today is always superior than $fecha */
    if($fecha == $todayString){
       return "Today"; 
    }  else if(date('Y',$dob) == date('Y',$current_time) && $age_months == "0") {
        return $age_days . " day".plural($age_days);
    } else if(date('Y',$dob) == date('Y',$current_time) && $age_days != "0") {
        return $age_months ." month".plural($age_months)." and ".$age_days ." day".plural($age_days);
    } else if(date('Y',$dob) == date('Y',$current_time) && $age_days == "0") {
        return $age_months . " month".plural($age_months);
    } else if($age_years != "0" &&  $age_months != "0" && $age_days == "0") {
        return $age_years . " year".plural($age_years)." and ".$age_months." month".plural($age_months);
    }  else if($age_years != "0" &&  $age_months == "0" && $age_days != "0") {
        return $age_years . " year".plural($age_years)." and ".$age_days." day".plural($age_days);
    } else if($age_years != "0" &&  $age_months == "0" && $age_days == "0") {
        return $age_years . " year".plural($age_years);
    } else { /* if you want to show the days always, add ----> ." and ".$age_days." day".plural($age_days) <---- below */
        return $age_years. " year".plural($age_years)." and ".$age_months." month".plural($age_months);
    } }
    
    function plural($num) {
    if($num!="1"){
        return "s";
    } }
    

    Ah, remember, maybe you need to set the timezone before.

    date_default_timezone_set("Europe/Madrid");
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥100 求三轴之间相互配合画圆以及直线的算法
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了
  • ¥15 链式存储应该如何解决
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站