Calculate age (jQuery or PHP) based on date of birth [not based on user input]

拜拜、爱过 提交于 2019-12-01 11:16:50

问题


I am looking for a JavaScript or PHP script that allows me to calculate somebody's age based on his/her date of birth in the mm/dd/yyyy format. I found this very helpful link "Calculate age in JavaScript" but it seems to be designed to work with user input. However, I am creating a page where I put the DOB directly into the HTML as simple text. Is it then still possible to calculate the age? Basically, I want this line of text to be the end-result:

29 (10/17/1983)

with the value of 29 being automatically computed and displayed as the age in years.

If something like this is possible, I would be very grateful for your help. I am fairly familiar with PHP and/or jQuery but I'm certainly not an advanced pro.


回答1:


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. You can obviously format the out to suit your needs.




回答2:


In javascript you can calculate it :

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:


Easiest way to calculate age is:

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

I.E. Years alive but -1 if you haven't had a birthday yet

MONTH DAY is 0301 for 1st March




回答4:


Javascript:

This probably does more than you are asking, but will give you the age in years, months, and days.

Like this: 8 years, 7 months, 21 days old

This works well if you have a site for a newborn only a few days old.

This is something I used on my daughters website almost 9 years ago

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


来源:https://stackoverflow.com/questions/15477086/calculate-age-jquery-or-php-based-on-date-of-birth-not-based-on-user-input

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!