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

我见过的所有功能都与出生日期有所不同,我希望在几个月内达到年龄 甚至几天。</ p>

php中的示例:</ p>

  $ date =“2015-05-23”; 
/ * * /
echo“有2个月零8天”;

$ date =“2012-10-30”;
/ 无论 /
echo“有2年零2个月”;
</ code > </ pre>

没关系,如果它在javascript / jquery或php中。</ p>

谢谢大家!</ p>
</ div>

展开原文

原文

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!

doukun8670
doukun8670 我强烈建议在这个时刻使用一个库。我想到了......JS中的日期很笨拙而且PHP并没有那么好
大约 5 年之前 回复
douye9175
douye9175 由于长度可变,做几个月会更困难,通过使用setDate或setUTCDate为0,您可以找出上个月的天数,这样您就可以找到任何月份的天数,所以如果你有一个负数月日差异,您可以找到要添加到此值的数字(并从差异中减去一个月)
大约 5 年之前 回复
duancan8382
duancan8382 stackoverflow.com/questions/1968167/...你所要做的就是现在使用而不是第二个日期,然后根据需要格式化你的输出。
大约 5 年之前 回复

3个回答

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");



您可以在PHP中使用此代码来获取从日期到当前日期或其他日期的天,月和年的差异</ p>

 &lt;?php 
$ date =“2015-07-17”;

$ difference = strtotime(date(“Ymd”)) - strtotime($ date );

$ days = abs(round($ difference / 86400,0));
$ years = abs(round($ days / 365,0));
$ months = abs(round($ 年/ 12,0));
echo“天数差异:$天|月:$月|年:$年”;
</ code> </ pre>

如果你想 把一个数字放在一个字符串中你可以使用stackoverflow中的这个答案...... </ p>

将数字转换为字符串 </ p>
</ div>

展开原文

原文

you can use this code in PHP to get the difference of days, months and years from a date to current date or other date

<?php
$date = "2015-07-17";

$difference = strtotime(date("Y-m-d")) - strtotime($date);

$days = abs(round($difference / 86400,0));
$years = abs(round($days /365,0));
$months = abs(round($years/12,0));
echo "Difference in Days: $days| Months: $months| Years: $years";

If you want to put a number in a string you can use this answer asked in stackoverflow...

Convert number to string



在php中你可以试试这个:</ p>

  $ date1 = date_create(“2013  -03-15“); 
$ date2 = date_create(”2013-12-12“);
$ diff = date_diff($ date1,$ date2);
echo($ diff-&gt; format(”%a “));
</ code> </ pre>

它将返回以天数表示的差异,这些天数也可以按月和年进行转换。</ p>
</ div>

展开原文

原文

In php you can try this:

$date1=date_create("2013-03-15");
$date2=date_create("2013-12-12");
$diff=date_diff($date1,$date2);
echo ($diff->format("%a"));

it will return difference in terms of days which can be converted in months and years too.

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问