0

I'm trying to calculate the age of a person. The result should only show years and days.

For example, when I enter the date 20.05.2000, it prints out 21 years and 18 days...

Highly appreciate any good solutions, thanks.

The code:

<h3>Enter birthdate</h3>

<form name="date" method="POST">
<input type="date" name="birthdate" 
        placeholder="dd-mm-yyyy">

 <input type="submit" name="submit" value="Submit">
</form>


<?PHP
  $birthdate = new DateTime($_POST['birthdate']);
  $today = new DateTime(date('d.m.y'));
 
  $diff = $today->diff($birthdate);

  if(isset($_POST['submit'])){

   echo "Years: " .$diff->y.", and Days: " .$diff -> d;

  }

?>
Sedrido
  • 23
  • 4
  • Hi, maybe this can help? $dteStart = new DateTime($strStart); $dteEnd = new DateTime($strEnd); $dteDiff = $dteStart->diff($dteEnd); print $dteDiff->format("%H:%I:%S"); Source : https://www.php.net/manual/tr/datetime.diff.php – İbrahim Sep 08 '21 at 13:22

0 Answers0