如何使用此公式计算年龄或将此公式用于我的编码:
$birthday = "1992-05-22";
$biday = new DateTime($birthday);
$today = new DateTime();
$diff = $today->diff($biday);
echo "BIRTHDAY: ". date('d M Y', strtotime($birthday)) .'<br />';
echo "AGE: ". $diff->y ." YO";
如果我从输入法得到new DateTime($birthday)
,这是我的编码:
<form name="form1" method="post" action="">
BDAY:<?php
echo "<select name='date'>";
echo "<option selected='selected'>date</option>";
for($a=1; $a<=31; $a+=1)
{
echo"<option value=$a> $a </option>";
}
echo "</select>";
echo "<select name='month'>";
echo "<option selected='selected'>month</option>";
$month=array("Januari","Februari","Maret","April","Mei","Juni","Juli","Agustus","September","Oktober","November","Desember");
$jlh_month=count($month);
for($c=0; $c<$jlh_bln; $c+=1)
{
echo"<option value=$month[$c]> $month[$c] </option>";
}
echo "</select>";
$now=date('Y');
echo "<select name='year'>";
echo "<option selected='selected'>year</option>";
for ($a=1990;$a<=$now;$a++)
{
echo "<option value='$a'>$a</option>";
}
echo "</select>";
?>
</p> <p>AGE :</label> <input type="text" name="age" id="age" value="(AGE HERE)" readonly></p>
我想在没有提交按钮的情况下在AGE文本字段上显示年龄。
答案 0 :(得分:0)
$("#calcCep").click(function(){
$.post("calc_frete.php", { ----> send to page php
age: $("#fieldage").val()
},
function(data, status) {
$("#freteValores").html(data); ---> Put in any field or DIV
});
});
一点ajax
答案 1 :(得分:0)
,而不是使用ajax ....这实际上是一些快速而肮脏的解决方案......所以你应该适应并改进这个......
//getBirtday.php
<?php
$age = "";
$data = $_POST['birthday'];
if(!empty($data)){
$birthday = $data['year'].'-'.$data['month'].'-'.$data['date'];
$biday = new DateTime($birthday);
$today = new DateTime();
$diff = $today->diff($biday);
$age = $diff->y;
}
echo $age;
?>
//test.php (get sure your jquery is loaded....)
<script>
$(document).ready(function(){
$('#year').on("change", function(){
birthday["date"] = $("#day").val();
birthday["month"] = $("#month").val();
birthday["year"] = $("#year").val();
$.ajax({
url: 'getBirthday.php',
method: "POST",
data: {birthday:birthday},
error: function(error) {
alert("ERROR"+error);
},
success: function(data) {
$("#age").val(data);
}
});
});
});
</script>
</p> <p>AGE :</label> <input type="text" name="age" id="age" value="<?=$age?>" readonly></p>
<form name="form1" method="post" action="#">
BDAY:<?php
echo "<select name='date' id='day'>";
echo "<option selected='selected'>date</option>";
for($a=1; $a<=31; $a+=1)
{
echo"<option value=$a> $a </option>";
}
echo "</select>";
echo "<select name='month' id='month'>";
echo "<option selected='selected'>month</option>";
$month=array(1=> "Januari",2 => "Februari", 3=> "Maret", 4=> "April", 5=> "Mei", 6=> "Juni", 7=>"Juli", 8=> "Agustus", 9=> "September", 10=>"Oktober",11=>"November",12=>"Desember");
for($c=0; $c<count($month); $c++)
{
echo "<option value=$c> $month[$c] </option>";
}
echo "</select>";
$now=date('Y');
echo "<select name='year' id='year'>";
echo "<option selected='selected'>year</option>";
for ($a=1990;$a<=$now;$a++)
{
echo "<option value='$a'>$a</option>";
}
echo "</select>";
?>
</form>