-3

How to Create a page in PHP with a date input field. In this field you choose A (birth) date. And also A submit button. With this button you give the browser dialog Age in years, months and days prelative to the system date.

Joey
  • 3
  • 1
  • 2

1 Answers1

0

Try This Code

    <?php
if(isset($_POST['age'])){
    $dob = $_POST['age'];
calculate_age($dob);
$year = calculate_Year($dob);
}

function calculate_age($birthday)
{
    $today = new DateTime();
    $diff = $today->diff(new DateTime($birthday));
    return printf('%d years, %d month, %d days', $diff->y, $diff->m, $diff->d);
}
function calculate_Year($birthday)
{
    $today = new DateTime();
    $diff = $today->diff(new DateTime($birthday));
    return  $diff->y;
}
 ?>

<form  method="post" action="age.php" >
ENTER DATE OF BIRTH : <input type="date" name="age" />
<input type="submit" value="Calculate Age" />
</form>

<?php if($year  > 20)
{?>
    <img src="old_image.png" />

<?php }else{?>
    <img src="young_image.png" />
<?php } ?>
Ehsan Ilahi
  • 298
  • 5
  • 15