How to make Age Counter through PHP ?

Description:- Through this example you can easily find out how many years, how many months, and how many days old are you. Just select the date and click on the button and it will reveal how exactly old are you.

<?php 
if(isset($_REQUEST['sub']))
{
//Defining variables in which we'll store the values of the given HTML fields
$dd=$_REQUEST['day'];
$mm = $_REQUEST['month']; 
$yy = $_REQUEST['year'];
$ddd=date_create("$yy-$mm-$dd");

//Defining variables in which we'll insert the current date
$rm=date('m');
$ry=date('Y');
$rd=date('d');
$zzz = date_create("$ry-$rm-$rd");

// Through date_diff() function we can calculate the difference of $ddd & &zzz
$dif = date_diff($ddd,$zzz);
$day= $dif->d;
$mon = $dif->m;
$yr = $dif->y;

$msg="You are exactly $yr years, $mon months & $day day old.";
}
?>
<!DOCTYPE html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<center>
<form action="<?php echo $_PHP_SELF; ?>" method="post">
    
    <select name="day">
        <option selected>Day </option>
            <?php
                for($a=1; $a<=31; $a++)
                {
                    echo "<option value=$a>".$a."</option>";
                }
              ?>
        </select>
        
        <select name="month">
            <option selected>Month</option>
            <?php
            $month=array("Jan", "Feb", "Mar", "April", "May", "June", "July", "Aug", "Sep", "Oct", "Nov", "Dec");
                foreach($month as $m)
                {
                    echo "<option value=$m>".$m."</option>";
                }
            ?>
        </select>
        <select name="year">
            <option selected>Year</option>
            <?php
                for($b=1970; $b<=2015; $b++)
                {
                    echo "<option value=$b>".$b."</option>";
                }
             ?>
        </select>
    <br/><br/>
<input type="submit" name="sub" value="Tell me how old I'm? "/>
</form>
<h1 style="color:rgba(0,102,255,1)"><?php echo $msg; ?> </h1>
</center>
</body>
</html>
Advertisements

One thought on “How to make Age Counter through PHP ?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s