Simple Age Calculator In PHP | CampCodes ...

Simple Age Calculator in PHP

May 30, 2020
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="bootstrap/css/bootstrap.min.css">
<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

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 *