Age Calculator In PHP

In this tutorial, I am going to show you about Age calculator. You can create an age calculator in PHP. Let's see how we create that. At first, create a PHP function name as getage() for calculating age. Then put this code into this function.

function getage($strdate)
$dob = explode("-",$strdate);
return 0;
$y = $dob[0];
$m = $dob[1];
$d = $dob[2];
return 0;
return 0;
return 0;
$y += 0;
$m += 0;
$d += 0;
if($y==0) return 0;
$rage = date("Y") - $y;
return $rage;

This function is ready to calculate age. Now it's time to create a script so that we can run this function and calculate age. Create a file name as index.php then write down this code.

// Here is getage function first
$date = '1992-09-12';
echo 'My age is '. getage($date);
Output will like this,
My age is 23

You should remember that this function is working in YYYY-MM-DD date format.
you can create online web age calculator app using this code. Here is an example about that.

// Put get age function first.
if (isset($_POST['day']) && isset($_POST['month']) && isset($_POST['year']))
$year = $_POST['year'];
$month = $_POST['month'];
$day = $_POST['day'];
$date = $year.'-'.$month.'-'.$day;
echo 'My age is '. getage($date);
echo '<b>Input Birthday</b><br/>';
echo '<form method="post">';
echo '<input name="year" size="5"> ';
echo '<select name="month">';
echo '<option value="01">January</option>';
echo '<option value="02">february</option>';
echo '<option value="03">March</option>';
echo '<option value="04">April</option>';
echo '<option value="05">May</option>';
echo '<option value="06">June</option>';
echo '<option value="07">July</option>';
echo '<option value="08">August</option>';
echo '<option value="09">September</option>';
echo '<option value="10">October</option>';
echo '<option value="11">November</option>';
echo '<option value="12">December</option>';
echo '</select>';
echo '<select name="day">';
echo '<option value="01">01</option>';
echo '<option value="02">02</option>';
echo '<option value="03">03</option>';
echo '<option value="04">04</option>';
echo '<option value="05">05</option>';
echo '<option value="06">06</option>';
echo '<option value="07">07</option>';
echo '<option value="08">08</option>';
echo '<option value="09">09</option>';
echo '<option value="10">10</option>';
echo '<option value="11">11</option>';
echo '<option value="12">12</option>';
echo '<option value="13">13</option>';
echo '<option value="14">14</option>';
echo '<option value="15">15</option>';
echo '<option value="16">16</option>';
echo '<option value="17">17</option>';
echo '<option value="18">18</option>';
echo '<option value="19">19</option>';
echo '<option value="20">20</option>';
echo '<option value="21">21</option>';
echo '<option value="22">22</option>';
echo '<option value="23">23</option>';
echo '<option value="24">24</option>';
echo '<option value="25">25</option>';
echo '<option value="26">26</option>';
echo '<option value="27">27</option>';
echo '<option value="28">28</option>';
echo '<option value="29">29</option>';
echo '<option value="30">30</option>';
echo '<option value="31">31</option>';
echo '</select>';
echo '<input type="submit" name="calculate">';
echo '</form>';
If you run this script, this script will execute like this,

if you select your birth year, month & day and press submit then this script will calculate and show your age.

This is a demo model of age calculator script. You can change and update this script any time. If you have any question about this script please comment here.