简体   繁体   中英

How to convert Birth date to age ( years, months, days ) and age( years, months, days ) to birth date conversion in PHP?

I have a form where the user can input a patient age in years, months and days. That will save the data in the database according to the patient's date of birth. The problem is I'm using strtotime() function to get the DOB of the patient, but when I want to show the age (years, months, days) from retrieving the date form the birthday column, sometimes it showing 1 or 2 days (+-) result. I'm using date_diff() function to retrieving the age (years, months, days). Any solution for my problem ?


$today = date("Y-m-d");
$dateOfBirth = strtotime("$today -2 years -2 months -20 days");
$birthday = date("Y-m-d", $dateOfBirth);

//and then tried to retrieve the age from that date which give me wrong answer

$diff = date_diff(date_create($birthday), date_create($today));

echo $diff->y; // showing 2
echo $diff->m; // showing 2
echo $diff->d; // showing 22 (it should be 20)

Update the following line in your code to this:

$diff = date_diff(date_create($today), date_create($birthday));

Test it here: http://sandbox.onlinephpfunctions.com/code/9dece465739e2315be32b7882ba3ca9a5d9d65b3

I hope you doing well, i read your question and found the following solution based on my research and my knowledge.

I hope this will helps you to resolve your problem.

=>To get the total year based on birthrate you can use the following code.

$bday = new DateTime('10.01.2001'); // Your date of birth
$today = new Datetime(date('m.d.y'));
$diff = $today->diff($bday);
printf(' Your age : %d years, %d month, %d days', $diff->y, $diff->m, $diff->d); //You will get your age

You can copy and paste the code in Fiddle to test the output. Using this you will definitely got your age by birth date.

Thanks in advance!

I wrote a simple function Calc_Age() to calculate this, it's very accurate and precise and I hope it's what you are looking for... below my code:

<!DOCTYPE html>

<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Patient Age Information</title>
</head>

<body>
<?php
  function Calc_Age($myday) {
    date_default_timezone_set('Europe/Rome');
    $birth = new DateTime($myday);
    $birth->format('Y-m-d');
    $today = new DateTime('NOW');
    $today->format('Y-m-d');
    $diffs = $today->diff($birth);
    $myage = $diffs->y . ($diffs->y == 1 ? ' year, ' : ' years, ');
    $myage .= $diffs->m . ($diffs->m == 1 ? ' month and ' : ' months and ');
    $myage .= $diffs->d . ($diffs->d == 1 ? ' day' : ' days');
    return $myage;
  }
?>



<h1>Patient: Angela Bennet</h1>

<p>Angela Bennet is <?php echo Calc_Age("1967-01-23"); ?> old</p>

<h1>Patient: Jhon Malkovich</h1>

<p>Jhon Malkovich is <?php echo Calc_Age("1977-04-14"); ?> old</p>

</body>

</html>

Result:

患者年龄信息

I hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM