Simple Age Calculator In PHP | Free Source Code

Simple Age Calculator in PHP

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>
<html>
<head>
    <meta charset="utf-8">
    <title>Simple Age Calculator in PHP</title>
    <link rel="stylesheet" type="text/css" href="bootstrap/css/bootstrap.min.css">
</head>
<body>
<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">
                    <label>Birthday:</label>
                    <input type="date" name="birthday" class="form-control" required>
                </div>
                <button type="submit" name="calculate" class="btn btn-primary">Calculate</button>
            </form>
 
            <?php
                if(isset($_POST['calculate'])){
                    $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>'; ?>
                    </div>
                    <?php
                }
 
            ?>
        </div>
    </div>
</div>
</body>
</html>

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
Facebook Comments
https://www.campcodes.com/

This is a free education portal. You can use every source code in your project without asking permission to the author. Share our website to everyone to make our community of programmers grow more.

Leave a Reply

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