PHP Age Calculator Script – With Demo

PHP Age Calculator Script – With Demo

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.

Recommended Posts:

  1. Autocomplete Search Box in PHP MySQL
  2. Compare Arrays PHP | PHP array_diff() Function
  3. Get, Write, Read, Load, JSON File from Url PHP
  4. Functions: Remove First Character From String PHP
  5. Remove Specific/Special Characters From String In PHP
  6. How to Replace First and Last Character From String PHP
  7. Reverse String in PHP
  8. Array Push, POP PHP | PHP Array Tutorial
  9. PHP Search Multidimensional Array By key, value and return key
  10. json_encode()- Convert Array To JSON | Object To JSON PHP
  11. PHP remove duplicates from multidimensional array
  12. PHP Remove Duplicate Elements or Values from Array PHP
  13. Get Highest Value in Multidimensional Array PHP
  14. PHP Get Min or Minimum Value in Array

AuthorAdmin

Greetings, I'm Devendra Dode, a full-stack developer, entrepreneur, and the proud owner of Tutsmake.com. My passion lies in crafting informative tutorials and offering valuable tips to assist fellow developers on their coding journey. Within my content, I cover a spectrum of technologies, including PHP, Python, JavaScript, jQuery, Laravel, Livewire, CodeIgniter, Node.js, Express.js, Vue.js, Angular.js, React.js, MySQL, MongoDB, REST APIs, Windows, XAMPP, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL, and Bootstrap. Whether you're starting out or looking for advanced examples, I provide step-by-step guides and practical demonstrations to make your learning experience seamless. Let's explore the diverse realms of coding together.

Leave a Reply

Your email address will not be published. Required fields are marked *