Compute Age in PHP Free Source code and Tutorial

Compute Age in PHP Free Source code and Tutorial

Introduction

This article will focus on creating a PHP script that will compute the age based on the date of birth. We will provide the source code as well as the explanation of the script. Let’s get started with the very simple and short coding session.

Objectives

By the end of this tutorial, you will be able to:

  1. Create a front-end form in Bootstrap that allows the user to enter or select the date of birth.
  2. To create a PHP script that process the user input and compute the age.
  3. To integrate and apply the source code in your projects.

Relevant Source code

Requirements

  • XAMPP
  • Text editor (VS Code, Sublime, Brackets), download and install a text editor of your choice
  1. Let us start first by create a function in PHP that will compute the age.
Compute Age in PHP Free Source code and Tutorial - function
Compute Age in PHP Free Source code and Tutorial – function
<?php
function getAge($dob){
$bday = new DateTime($dob);
$today = new DateTime(date('m.d.y'));
if($bday > $today){
return 'You are not born yet';
}
$diff = $today->diff($bday);
return 'Your Age is: '.$diff->y.' Years Old';
}
?>

Line 19-29 – this is the scope of the function. The name of our function is getAge with one parameter.

Line 21 – we have created a variable $bday that will hold your date of birth info.

Line 22 – the variable $today is to hold the current date value.

diff() function is an inbuilt function in PHP which is used to return the difference between two given DateTime objects.

This function will compute the age of the person based on the birthdate selected using the date picker.

  1. We will next create the Form that includes the date picker and button.
Compute Age in PHP Free Source code and Tutorial - form
Compute Age in PHP Free Source code and Tutorial – form
<form>
<div class="input-wrapper">
<label>Date of Birth</label>
<input type="date" name="dob" value="<?php echo (isset($_GET['dob']))?$_GET['dob']:''; ?>">
<input type="submit" value="Calculate Age">
</div>
</form>

Line 30-36 – this is the form used to capture the user input and submit it to the server for processing. It includes two components; a date picker and a submit button. The date selected will be submitted to the function that we have created in step 1 for processing.

  1. The last step is to create a PHP script that will call our function and display the age of the person based on the selected date in the form.
Compute Age in PHP Free Source code and Tutorial - display age
Compute Age in PHP Free Source code and Tutorial – display age
<?php
if (isset($_GET['dob']) && $_GET['dob']!=''){?>
<h4><?php echo getAge($_GET['dob']); ?></h4>
<?php } ?>
Compute Age in PHP Free Source code and Tutorial - output
Compute Age in PHP Free Source code and Tutorial – output

Complete Source code

<!DOCTYPE html>
<html>

<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, shrink-to-fit=no">
<title>Compute Age in PHP</title>
<link rel="stylesheet" href="assets/bootstrap/css/bootstrap.min.css">
</head>

<body>
<div class="row">
<div class="col-md-8 offset-md-2">
<div class="card">
<div class="card-header bg">
<h1 class="center">Compute Age in PHP</h1>
</div>
<div class="card-body">
<?php
function getAge($dob){
$bday = new DateTime($dob);
$today = new DateTime(date('m.d.y'));
if($bday > $today){
return 'You are not born yet';
}
$diff = $today->diff($bday);
return 'Your Age is: '.$diff->y.' Years Old';
}
?>
<form>
<div class="input-wrapper">
<label>Date of Birth</label>
<input type="date" name="dob" value="<?php echo (isset($_GET['dob']))?$_GET['dob']:''; ?>">
<input type="submit" value="Calculate Age">
</div>
</form>
<?php
if (isset($_GET['dob']) && $_GET['dob']!=''){?>
<h4><?php echo getAge($_GET['dob']); ?></h4>
<?php } ?>
</div>
</div>
</div>
<script src="assets/js/jquery.min.js"></script>
<script src="assets/bootstrap/js/bootstrap.min.js"></script>
</body>
</html>

FREE DOWNLOAD SOURCE CODE

FREE DOWNLOAD PDF TUTORIAL

Summary

PHP has a built-in function called diff() that can be used to calculate the age of a person. The function takes two parameters: the first is the date of birth, and the second is the current date. The function returns the age in years. To use the function, you first need to convert the dates into a format that PHP can understand. In this tutorial, we have learned how to create a function, create a form that accepts input and lastly is to call the function and display the age.

We hope you found this tutorial to be helpful! Wishing you the best of luck with your projects! Happy Coding!

You may visit our Facebook page for more information, inquiries, and comments. Please subscribe also to our YouTube Channel to receive  free capstone projects resources and computer programming tutorials.

Hire our team to do the project.

Related Topics and Articles:

Calculate Sum of Column in PHP and MySQL

Dynamic Drop down in PHP and MySQL

, , , , , , , , , , , , , , , ,

Post navigation