使用datepicker无法正确计算年龄

时间:2016-06-25 17:03:44

标签: javascript php jquery datepicker

我从datepicker中选择日期时无法计算年龄。 它总是显示一些错误的年龄。年龄计算代码用age.php编写,而其余代码在index.php中。而且当我在年龄strtotime()中写出1980-10-01这样的年龄时,它显示了正确的答案,但我希望用户选择年龄,因此我在其中编写了post变量。我使用的是PHP 5.2,无法升级,所以请给我一个有效的PHP 5.2年龄计算方法。谢谢。



$(document).ready(function() {
  $('#datetimepicker8').datepicker({
    format: 'dd-mm-yyyy',
    icons: {
      time: "fa fa-clock-o",
      date: "fa fa-calendar",
      up: "fa fa-arrow-up",
      down: "fa fa-arrow-down"
    }
  });
  $(document).on('change', '#birthdate', function() {
    $.ajax({
      url: 'age.php',
      data: {
        action: 'birthage'
      },
      type: 'post',
      success: function(data) {
        $("#age").val(data);
      }
    });
  });
});

age.php 

<?php function birthage() {
  $age="";
  $age=floor((time() - strtotime($_POST['birtdate'])) / 31556926);
  echo $age;
}
//here you can do some "routing"
$action=$_POST['action']; //remember to escape it
 switch ($action) {
  case'birthage': birthage();
  break;
  default: //function not found, error or something
  break;
}
?>
&#13;
index.php
<div class="form-group row">
  <label class="col-sm-2 form-control-label">Birthdate</label>
  <div class="col-sm-5">
    <div class='input-group date' id='datetimepicker8'>
      <input type='text' id="birthdate" class="form-control" placeholder="D.O.B" name="birthdate" />
      <span class="input-group-addon">
          <i class="fa fa-calendar" aria-hidden="true"></i>
        </span>
    </div>
  </div>
  <label class="col-sm-1 form-control-label">Age:</label>
  <div class="col-sm-4">
    <input type="text" id="age" class="form-control" name="age" placeholder="Age">
  </div>
</div>
&#13;
&#13;
&#13;

0 个答案:

没有答案