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>
Aucun commentaire:
Enregistrer un commentaire