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条)

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!