dimanche 8 août 2021

PHP Age Calculating

I have tried to get the age of a person, using the code below, although I wanted the output to be your exact age, it reads an incorrect age if your birthday was before the current date but a correct age if your birthday is after the current date kindly correct the code

<?php
   $name = $_POST['name'];
   $month = $_POST['month']; 
   $day = $_POST['day'];  
   $year = $_POST['year'];
   $age = date('Y') - $year - 1 ;
   if (date('F') <= $month) {
        $age = date('Y') - $year;
   } 
   $nos = $_POST['nos'];
   $gender = $_POST['gender'];
   $loa = $_POST['loa'];

   echo "<div style ='font:21px Arial,tahoma,sans-serif;color:#7a1ac4'><b>`Name:`</b> $name</div>";
   echo "<div style ='font:21px Arial,tahoma,sans-serif;color:#7a1ac4'><b>`Birth Date:`</b> 
   $month-$day-$year </div>";
   echo "<div style ='font:21px Arial,tahoma,sans-serif;color:#7a1ac4'><b>`Age:`</b> aprox. 
   $age</div>";
   echo "<div style ='font:21px Arial,tahoma,sans-serif;color:#7a1ac4'><b>`Name of School:`</b> 
   $nos</div>";
   echo "<div style ='font:21px Arial,tahoma,sans-serif;color:#7a1ac4'><b>`Gender:`</b> 
   $gender</div>";
   echo "<div style ='font:21px Arial,tahoma,sans-serif;color:#7a1ac4'><b>`Level of Academics:`</b> 
   $loa</div>";
?>

Aucun commentaire:

Enregistrer un commentaire