In this tutorial, we will show you how to calculate age from date of birth in PHP.
How to calculate age from Date of Birth in PHP?
Here are steps to create age calculator in PHP:
#1 Create Age Calculator PHP file
First of all, we need to create a PHP form where users can select the date, month, and year, and click on submit button to calculate age.
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>
#2 Display Calculated 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.
Recommended Posts:
- Autocomplete Search Box in PHP MySQL
- Compare Arrays PHP | PHP array_diff() Function
- Get, Write, Read, Load, JSON File from Url PHP
- Functions: Remove First Character From String PHP
- Remove Specific/Special Characters From String In PHP
- How to Replace First and Last Character From String PHP
- Reverse String in PHP
- Array Push, POP PHP | PHP Array Tutorial
- PHP Search Multidimensional Array By key, value and return key
- json_encode()- Convert Array To JSON | Object To JSON PHP
- PHP remove duplicates from multidimensional array
- PHP Remove Duplicate Elements or Values from Array PHP
- Get Highest Value in Multidimensional Array PHP
- PHP Get Min or Minimum Value in Array