年龄计算器出了问题

时间:2017-04-27 08:18:28

标签: php

代码1: 代码1工作正常,但当我试图通过从用户获取值并输出答案我无法做到这个代码我的html请帮助我完全迷茫

#Code 1
<?php
$dateOfBirth = "25-05-1994";
$today       = date("Y-m-d");
$diff        = date_diff(date_create($dateOfBirth), date_create($today));
echo 'Age is' . $diff->format('%yYears, %mMonths, %dDays');
?>

#code 2
<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8">
    <title>Excel To HTML using codebeautify.org
    </title>
  </head>
  <body>
    <!DOCTYPE HTML>
    <html>
      <head>
        <title>Age calculator
        </title>
      </head>
      <body>
        <?php
if(isset($_POST['submit'])){
$dateOfBirth = $_POST['dateOfBirth'];
$today = date("Y-m-d");
$diff = date_diff(date_create($dateOfBirth, $today));
}
echo 'Age is' .  $diff->format('%yYears, %mMonths, %dDays');
?>
        <form method="post" action="">
          <input type="text" name="dateOfBirth"/>
          <input type="submit" value="submit" name="submit" />
        </form>
      </body>
    </html>
  </body>
</html>

1 个答案:

答案 0 :(得分:2)

在您的代码2中更改

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

$diff = date_diff(date_create($dateOfBirth), date_create($today));就像你在代码1上做的那样