How to calculate age using php


index.php


     

<!DOCTYPE html>

<html>

<head>

<meta charset="UTF-8">

<title>How to calculate age in php </title>

</head>

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>

<body>



  <?php

  $dateOfBirth = "17-09-1996";

  $today = date("Y-m-d");

  $diff = date_diff(date_create($dateOfBirth), date_create($today));

  echo 'Age is '.$diff->format('%y') ."Year " .$diff->format('%m').' Month '.$diff->format('%d')." Days";

  ?>  

</body>

</html>

    
 
 

for some Date difference format .


     

      

      <?php

       $diff->format('%y'); // For Year

       $diff->format('%m'); // For Month

       $diff->format('%d'); // For Day

       $diff->format('%h'); // For Hours

       $diff->format('%i'); // for year

       $diff->format('%s'); // For Second

       $diff->format('%a'); // Number of Days

      ?>