根据出生日期[不基于用户输入]计算年龄(jQuery或PHP)

时间:2013-03-18 12:40:36

标签: javascript php jquery date-math dob

我正在寻找一个JavaScript或PHP脚本,它允许我根据他/她的出生日期以mm/dd/yyyy格式计算某人的年龄。我发现这个非常有用的链接“Calculate age in JavaScript”,但它似乎是为了处理用户输入而设计的。但是,我正在创建一个页面,我将DOB直接放入HTML作为简单文本。那么还可以计算年龄吗?基本上,我希望这行文本成为最终结果:

29 (10/17/1983)

自动计算29的值并显示为年龄。

如果有可能这样的话,我将非常感谢你的帮助。我对PHP和/或jQuery非常熟悉,但我当然不是高级专业人士。

4 个答案:

答案 0 :(得分:7)

PHP:

$today = new DateTime();
$birthdate = new DateTime("1973-04-18 09:48:00");
$interval = $today->diff($birthdate);
echo $interval->format('%y years');

See it in action。显然,您可以根据自己的需要对其进行格式化。

答案 1 :(得分:1)

使用Javascript:

这可能比你要求的更多,但会给你年龄,月和日的年龄。

像这样:8 years, 7 months, 21 days old

如果你的新生儿网站只有几天,那么效果很好。

这是我大约9年前在女儿网站上使用的东西

    function GetMyAge()
{
<!--

birthTime = new Date("July 25, 2004 00:00:00 GMT-0600")
todaysTime = new Date();

<!-- Parse out specific date values
todaysYear = todaysTime.getFullYear()
todaysMonth = todaysTime.getMonth()
todaysDate = todaysTime.getDate()
todaysHour = todaysTime.getHours()
todaysMinute = todaysTime.getMinutes()
todaysSecond = todaysTime.getSeconds()
birthYear = birthTime.getFullYear()
birthMonth = birthTime.getMonth()
birthDate = birthTime.getDate()
birthHour = birthTime.getHours()
birthMinute = birthTime.getMinutes()
birthSecond = birthTime.getSeconds()

<!-- Adjusts for Leap Year Info
if ((todaysYear / 4) == (Math.round(todaysYear / 4))) {
   countLeap = 29}
else {
     countLeap = 28}

<!-- Calculate the days in the month
if (todaysMonth == 2) {
   countMonth = countLeap}
else {
     if (todaysMonth == 4) {
        countMonth = 30}
     else {
        if (todaysMonth == 6) {
           countMonth = 30}
        else {
           if (todaysMonth == 9) {
              countMonth = 30}
           else {
              if (todaysMonth == 11) {
                 countMonth = 30}
              else {
                 countMonth = 31}}}}}

<!-- Doing the subtactions
if (todaysMinute > birthMinute) {
   diffMinute = todaysMinute - birthMinute
   calcHour = 0}
else {
   diffMinute = todaysMinute + 60 - birthMinute
   calcHour = -1}
if (todaysHour > birthHour) {
   diffHour = todaysHour - birthHour + calcHour
   calcDate = 0}
else {
   diffHour = todaysHour + 24 - birthHour  + calcHour
   calcDate = -1}
if (todaysDate > birthDate) {
   diffDate = todaysDate - birthDate + calcDate
   calcMonth = 0}
else {
   diffDate = todaysDate + countMonth - birthDate  + calcDate
   calcMonth = -1}
if (todaysMonth > birthMonth) {
   diffMonth = todaysMonth - birthMonth + calcMonth
   calcYear = 0}
else {
   diffMonth = todaysMonth + 12 - birthMonth + calcMonth
   calcYear = -1}
diffYear = todaysYear - birthYear + calcYear

<!-- Making sure it all adds up correctly
if (diffMinute == 60) {
   diffMinute = 0
   diffHour = diffHour + 1}
if (diffHour == 24) {
   diffHour = 0
   diffDate = diffDate + 1}
if (diffDate == countMonth) {
   diffDate = 0
   diffMonth = diffMonth + 1}
if (diffMonth == 12) {
   diffMonth = 0
   diffYear = diffYear + 1}

if (diffYear != 1)
    YearPlural = "s"
else
    YearPlural=""

if (diffMonth != 1)    
    MonthPlural = "s"
else
    MonthPlural=""

if (diffDate != 1)     
    DatePlural = "s"
else
    DatePlural=""


if (diffYear == 0 && diffMonth == 0)
    return (diffDate + ' day' + DatePlural + ' old.  ');
else if (diffYear == 0)
    return (diffMonth + ' month' + MonthPlural + ', ' + diffDate + ' day' + DatePlural + ' old.  ');
else
    return (diffYear + ' year' + YearPlural + ', ' + diffMonth + ' month' + MonthPlural + ', ' + diffDate + ' day' + DatePlural + ' old.  ');
}
// -->

答案 2 :(得分:1)

在javascript中你可以计算出来:

function getAge(dateString) {
    var today = new Date();
    var birthDate = new Date(dateString);
    var age = today.getFullYear() - birthDate.getFullYear();
    var m = today.getMonth() - birthDate.getMonth();
    if (m < 0 || (m === 0 && today.getDate() < birthDate.getDate())) {
        age--;
    }
    return age;
}

答案 3 :(得分:1)

计算年龄的最简单方法是:

CURRENT YEAR - BIRTH YEAR = AGE  
IF(CURRENT MONTH DAY < BIRTH MONTH DAY) AGE--;

即。如果你还没有过生日那么活着但是-1岁

  

3月1日的月末是0301