0

我正在尝试计算年龄。我正在使用此代码,它适用于示例生日,但我无法弄清楚如何让 $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;
?>
4

1 回答 1

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 提交的值以仅允许有效日期

于 2013-10-17T16:59:10.343 回答