How to create age calculator program in php

 

Agecalculator program

In the below example Create a HTML script to take input from users separated by "/" current(year, month, date) and his date of birth (year, month, date). Convert the inputted value mm/dd/yyyy string into array using explode( ) function for both current date and date of birth. strore these values in $arr variable at 0 index date, at 1 index month and at 2 index year like $arr[0]=date $arr[1]=month $arr[2]=year same as for (date of birth) inside $brr variable. Now check for date if current "date" is less than date of birth "date" then borrow 1 month and add 30days in current date and calculate. Same as for Month here borrow 1year from existing year and add 12 months. subtract current months with date of birth "month" and store in $mon variable. at last calculate for year and store the value in $yr variable. Display the all calculated values(Year,Month, Date) like: 26 years 4 months 0 days


<?php

error_reporting(1);

$day=0;

$yr=0;

$mon=0;

if(isset($_POST['b1']))

{

$d1=$_POST['t1'];

$d2=$_POST['t2'];

$arr=explode("/",$d1);

$brr=explode("/",$d2);

if($arr[0]<$brr[0])

{

$arr[0]+=30;

$arr[1]-=1;

}

$day=$arr[0]-$brr[0];

if($arr[1]<$brr[1])

{

$m1+=12;

$arr[2]-=1;

}

$mon=$arr[1]-$brr[1];

$yr=$arr[2]-$brr[2];

}

?>

<html>

<body>

<form method="post">

<table border="2">

<tr>

<td align="center" colspan="2"><font color="orange"><h2><b>Age Calculator</b></h2></font></td>

</td>

<tr>

<td align="center"><b>enter current date:</b></td>

<td align="center"><input type="text" name="t1" autofocus></td>

</tr>

<tr>

<td align="center"><b>enter your DOB:</b></td>

<td align="center"><input type="text" name="t2"></td>

</tr>

<tr>

<td align="center" colspan="2"><input type="submit" name="b1" value="calculate"></td>

</tr>

<tr>

<td align="center"><b>Your Age is:</b></td>

<td align="center">

<?php 

error_reporting(1);

echo '<font color="blue" size="5">';

echo $yr.' years '.$mon.' months '.$day.' days ';

echo '</font>';

?>

</td>

</tr>

</table>

</form>

</body>

</html>

Comments

Popular posts from this blog

How to create Animated 3d chart with R.

Linux/Unix Commands frequently used

R Programming Introduction