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
JavaScript
x
<?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>";
?>
Advertisement
Answer
You can use this for age calculation:
JavaScript
$today = new DateTime("now");
$birthdate = new DateTime("$year-$month-$day");
$age = $today->diff($birthdate)->y;
Update:
So the whole code with some cleanup will be like:
JavaScript
<?php
$name = $_POST['name'];
$month = $_POST['month'];
$day = $_POST['day'];
$year = $_POST['year'];
$today = new DateTime("now");
$birthdate = new DateTime("$year-$month-$day");
$age = $today->diff($birthdate)->y;
$nos = $_POST['nos'];
$gender = $_POST['gender'];
$loa = $_POST['loa'];
?>
<div style ='font:21px Arial,tahoma,sans-serif;color:#7a1ac4'>
<b>`Name:`</b>
<?php echo $name; ?>
</div>;
<div style ='font:21px Arial,tahoma,sans-serif;color:#7a1ac4'>
<b>`Birth Date:`</b>
<?php echo $birthdate->format('Y-m-d'); ?>
</div>;
<div style ='font:21px Arial,tahoma,sans-serif;color:#7a1ac4'>
<b>`Age:`</b> aprox.
<?php echo $age; ?>
</div>;
<div style ='font:21px Arial,tahoma,sans-serif;color:#7a1ac4'>
<b>`Name of School:`</b>
<?php echo $nos; ?>
</div>;
<div style ='font:21px Arial,tahoma,sans-serif;color:#7a1ac4'>
<b>`Gender:`</b>
<?php echo $gender; ?>
</div>;
<div style ='font:21px Arial,tahoma,sans-serif;color:#7a1ac4'>
<b>`Level of Academics:`</b>
<?php echo $loa; ?>
</div>;