PHP Age Calculator Script – With Demo

Calculate your or someone’s age is little difficult. There are not only have to do addition and subtraction Because many months have 30 days and many have 31 and February has 28 days.  Creating an age calculator program might be tricky with most web languages.

But in PHP this program is very easy to create with the non-tricky method. Previously I had shared simple PHP calculator, but this time I am sharing PHP Age Calculator. I can also create this program with JavaScript, Maybe I will share in the future. The main reason I had created this program using PHP is very simple and fewer line of codes.

This PHP Age Calculator program is very simple, even a beginner can understand it easily. It has very fewer codes of PHP, maybe 20-25 lines of codes maximum. If you don’t have knowledge in PHP, I think after reading 3-4 times codes understand this program. Not any other PHP program.

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 | CodeOne.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.

Leave a Reply

Your email address will not be published.