dongtu7205 2016-06-25 17:03
浏览 79

使用datepicker无法正确计算年龄

I just cannot calculate the age when I select a date from the datepicker. It is always showing some wrong age.The age calculation code is written in age.php while the rest of the code is in index.php. And also when I write an age like 1980-10-01 in the age strtotime()it shows the correct answer but I want the user to select the age hence I have written the post variable in it. I am using php 5.2 and cannot upgrade it so please give me a working php 5.2 age calculation method.Thanks in advance.

$(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;
}
?>
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>

</div>
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 微信会员卡接入微信支付商户号收款
    • ¥15 如何获取烟草零售终端数据
    • ¥15 数学建模招标中位数问题
    • ¥15 phython路径名过长报错 不知道什么问题
    • ¥15 深度学习中模型转换该怎么实现
    • ¥15 HLs设计手写数字识别程序编译通不过
    • ¥15 Stata外部命令安装问题求帮助!
    • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
    • ¥15 TYPCE母转母,插入认方向
    • ¥15 如何用python向钉钉机器人发送可以放大的图片?