2013-03-18 85 views
1

我在尋找一個JavaScript或PHP腳本,它允許我根據他/她的出生日期計算出某人的年齡爲mm/dd/yyyy格式。我發現這個非常有用的鏈接「Calculate age in JavaScript」,但它似乎被設計爲與用戶輸入一起工作。但是,我正在創建一個頁面,將DOB直接放入HTML中作爲簡單文本。是否仍然有可能計算年齡?基本上,我想這行文字是最終結果:根據出生日期計算年齡(jQuery或PHP)[不基於用戶輸入]

29 (10/17/1983) 

29價值被自動計算,並隨着年齡的年顯示。

如果這樣的事情是可能的,我會非常感謝您的幫助。我對PHP和/或jQuery非常熟悉,但我當然不是高級專業人員。

回答

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

這不會工作,如果我的生日,它的00:00:00和09之間: 47:59。您需要將時間設置爲00:00:00 – Waygood 2013-03-18 13:00:01

+0

非常感謝!還有一個問題,這個函數是閏年的嗎? – rf2012 2013-03-18 13:28:41

+0

它的確如此。 DateTime類自動處理大量日期/時間怪癖,如閏年。 – 2013-03-18 13:36:33

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. '); 
} 
// --> 
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; 
} 
1

計算年齡的最簡單方法是:

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

IE年還活着,但-1,如果您還沒有一個生日尚未

月日是0301 3月1日