Calculate Age from Date of Birth (DOB) using PHP

We often need to calculate age of an individual based on his/her date of birth in normal web applications. The basic idea is to calculate the difference in days, months and years between current date and provided date of birth to calculate the age.

Following is the complete source code to calculate the age based on date of birth :

Full Source Code :

  margin:0 auto;
  background: #fff;
  box-shadow: 0 3px 10px rgba(140,152,164,0.3);
  margin-bottom: 20px;
.input-wrapper label{
  font-size: 20px;
    font-weight: bold;
    padding: 10px;
    background: #609460;
    color: #fff;
  background: #eee;
  text-align: center;

function getAge($dob){
  $bday = new DateTime($dob);
  $today = new Datetime(date('m.d.y'));
    return 'You are not born yet';
  $diff = $today->diff($bday);
  return 'Your Current Age is : '.$diff->y.' Years, '.$diff->m.' month, '.$diff->d.' days';

<h1 class="center">Calculate Age from DOB using PHP</h1>
<div class="form-wrapper">
    <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">
    if(isset($_GET['dob']) && $_GET['dob']!=''){?>
      <div class="result-wrapper">
        <?php echo getAge($_GET['dob']);?>
    <?php }

Follow this video for complete guidance :

Recommended For You

About the Author: Admin