Simple Age Calculator In PHP

age calculator in php

Getting Started

I’ve used Bootstrap in this tutorial which is included in the downloadable of this tutorial. If you want, you can download Bootstrap using this link.

Creating our Calculator

Here’s the full PHP code of our calculator.

<!DOCTYPE html>
    <meta charset="utf-8">
    <title>Simple Age Calculator in PHP</title>
    <link rel="stylesheet" type="text/css" href="">
<div class="container">
    <h1 class="page-header text-center">Simple Age Calculator</h1>
    <div class="row">
        <div class="col-sm-4 col-sm-offset-4">
            <form method="POST">
                <div class="form-group">
                    <input type="date" name="birthday" class="form-control" required>
                <button type="submit" name="calculate" class="btn btn-primary">Calculate</button>
                    $bday = $_POST['birthday'];
                    $today = date('Y-m-d');
                    $diff = date_diff(date_create($bday), date_create($today));
                    <div class="alert alert-info text-center" style="margin-top:20px;">
                        <?php echo 'Age is <b>'.$diff->format('%y').'</b>'; ?>

In this code, if we set $_POST[‘calculate’] which is our submit button, we get $bday and $today. Then using date_diff(), we determine the age and echo it back to our user.

That ends this tutorial. Happy Coding!

Download Here

Sharing is caring!

Facebook Comments

Leave a Reply