简体   繁体   中英

PHP calculate age with DateTime::diff

How to calculate age with this formula or put this formula to my coding:

$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";

If my new DateTime($birthday) I get from input method, here is my coding:

<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>

And i want to display age on AGE text field without submit button.

                        $("#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
                            });
                        });

a little of ajax

than, use ajax instead.... this is really some quick and dirty solution...so you should adapt and improve this..

//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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM