简体   繁体   English

如何将出生日期转换为年龄(年,月,日)和年龄(年,月,日)到出生日期转换为PHP?

[英]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. 问题是我正在使用strtotime()函数来获取患者的DOB,但是当我想显示从生日列中检索日期的年龄(年,月,日)时,有时会显示1或2天(+ - )结果。 I'm using date_diff() function to retrieving the age (years, months, days). 我正在使用date_diff()函数来检索年龄(年,月,日)。 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 在这里测试: 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. 您可以在Fiddle中复制并粘贴代码以测试输出。 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: 我写了一个简单的函数Calc_Age()来计算它,它非常准确和精确,我希望它是你要找的...在我的代码下面:

<!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. 我希望这有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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