如何将出生日期转换为年龄并将其保存到表格中?

时间:2014-04-02 09:05:38

标签: php mysql

我正在为我们的家庭成员创建一个在线记录。

我有两个表Member和MemberDetails,并且BirthDate被插入'成员'表。我想使用存储在表格'会员'中的这个BirthDate来计算年龄。并将其存储在表格“成员详细信息”中,用于“年龄'。

”列下的每个成员

3 个答案:

答案 0 :(得分:2)

您可以直接在SQL中执行此操作,

尝试类似

的内容
INSERT INTO Member_Detail (id, dob)
VALUES SELECT id, TIMESTAMPDIFF(YEAR, dob, CURDATE()) FROM Member

答案 1 :(得分:0)

试试这个 -

<?php
$birthDate = "05-03-1991";  // m-d-Y
$birthDate = explode("-", $birthDate);

$age = (date("md", date("U", mktime(0, 0, 0, $birthDate[0], $birthDate[1], $birthDate[2]))) > date("md")
? ((date("Y") - $birthDate[2]) - 1)
: (date("Y") - $birthDate[2]));

echo "Your age is:" . $age;

答案 2 :(得分:0)

示例: -

<?php
  //date in mm/dd/yyyy format; or it can be in other formats as well
  $birthDate = "12/17/1983";
  //explode the date to get month, day and year
  $birthDate = explode("/", $birthDate);
  //get age from date or birthdate
  $age = (date("md", date("U", mktime(0, 0, 0, $birthDate[0], $birthDate[1], $birthDate[2]))) > date("md")
    ? ((date("Y") - $birthDate[2]) - 1)
    : (date("Y") - $birthDate[2]));
  echo "Age is:" . $age;
?>

或参考此链接 PHP calculate age