PHP age calculator program with source code and live demo. This tutorial shows you how to implement an age calculator in PHP.
This tutorial also provides you the easy way to implement age calculator in PHP with a demo and full source code of the PHP age calculator.
PHP Age Calculator Program
First of all, we need to create a PHP form where you can select the date, month and year. After that, you submit the form, this PHP program will calculate a person’s current age.
Automatically calculate the age after selecting the date of birth date in PHP. In this PHP age calculator script, we will use the $_POST method. And this program calculates the age of person and store in variables.
Create one PHP file
Now we need to create one PHP file that name “age-calculator.php” and update the below code into this:
<!DOCTYPE html>
<html>
<head>
<title>PHP Age Calculator | Tutsmake.com</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css">
</head>
<body>
<div class="container">
<div class="card">
<div class="card-header">
PHP Age Calculator
</div>
<div class="card-body">
<form action="age-calculator.php" method="post" class="form-group">
<div class="row mb-3">
<div class="col-md-4">
<select name="day" class="form-control">
<?php
for($i=1;$i<=31;$i++)
{
echo "<option value='$i'>$i</option>";
}
?>
</select>
</div>
<div class="col-md-4">
<select name="month" class="form-control">
<?php
for($i=1;$i<=12;$i++)
{
echo "<option value='$i'>$i</option>";
}
?>
</select>
</div>
<div class="col-md-4">
<select name="year" class="form-control">
<?php $year = date('Y'); ?>
<?php
for($i=1900;$i<=$year;$i++)
{
echo "<option value='$i'>$i</option>";
}
?>
</select>
</div>
</div>
<div class="row">
<div class="col-md-4">
<input type="submit" name="submit" class="btn btn-primary " value="Click to calculate age">
</div>
</div>
</form>
</div>
<div class="card-footer">
<?php
if(isset($_POST['submit'])) {
$day=$_POST['day'];
$month=$_POST['month'];
$year=$_POST['year'];
$dob=$day.'-'.$month.'-'.$year;
$bday=new DateTime($dob);
$age=$bday->diff(new DateTime);
$today=date('d-m-Y');
echo '<br />';
echo '<b>Your Birth date: </b>';
echo $dob;
echo '<br>';
echo '<b>Your Age : </b> ';
echo $age->y;
echo ' Years, ';
echo $age->m;
echo ' Months, ';
echo $age->d;
echo ' Days';
}
?>
</div>
</div>
</div>
</body>
</html>
The main PHP script to calculate and display age
When you select a date, month, and year in the form. After submitting the form. The PHP script will execute and display the result of your selected dob. It’s mainly used DateTime() and date diff() function of PHP to calculate the age.
<?php
if(isset($_POST['submit'])) {
$day=$_POST['day'];
$month=$_POST['month'];
$year=$_POST['year'];
$dob=$day.'-'.$month.'-'.$year;
$bday=new DateTime($dob);
$age=$bday->diff(new DateTime);
$today=date('d-m-Y');
echo '<br />';
echo '<b>Your Birth date: </b>';
echo $dob;
echo '<br>';
echo '<b>Your Age : </b> ';
echo $age->y;
echo ' Years, ';
echo $age->m;
echo ' Months, ';
echo $age->d;
echo ' Days';
}
?>
After that, you can hit the below URL in your browser:
http://localhost/age-calculator.php
That’s all. Now we have successfully created a PHP age calculator program. and also you can modify this as your requirement.