从数据库$ day,$ month,$ year计算年龄

时间:2013-10-17 16:52:09

标签: php

我正在尝试计算年龄。我正在使用这段代码,它适用于样本生日,但我无法弄清楚如何让$ birthDate等于我的三个单独的变量$ day,$ month,$ year,存储在我的数据库中为每个用户。我怎么这样做$ birthDate = $ day / $ month / $ year并使用下面的年龄计算器?

<?php
$birthDate = "12/17/1983";
$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 "Age is:".$age;
?>

1 个答案:

答案 0 :(得分:3)

这是在我的注册系统中试用和测试的

 //date in mm/dd/yyyy format; or it can be in other formats as well
  $birthDate = $_POST['month']."/".$_POST['date']."/".$_POST['year'];
  //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]));

使用日期时间选择器或验证通过javascript提交的值以仅允许有效日期