How to create an Age Calculator? Today you will learn how to calculate age using PHP with PHP Age Calculator Program.
Calculate your or someone’s age is little difficult. There are not only have to do addition and subtraction Because many months have 30 days and many have 31 and February has 28 days. Creating an age calculator program might be tricky with most web languages.
But in PHP this program is very easy to create with the non-tricky method. Previously I had shared Simple PHP calculator, but this time I am sharing PHP Age Calculator. I can also create this program with JavaScript, Maybe I will share in the future. The main reason I had created this program using PHP is very simple and fewer line of codes.
This PHP Age Calculator program is very simple, even a beginner can understand it easily. It has very fewer codes of PHP, maybe 20-25 lines of codes maximum. If you don’t have knowledge in PHP, I think after reading 3-4 times codes You can understand this program. Not any other PHP program.
If you are thinking right now how this program will work? Let’s take a look at this preview given below. Then you will fully understand how this program actually is and how it works.
Preview Of Calculate Age Program
See this video preview for getting an idea about this program.
Now you can see how this program works. If you like this then get the source code of this program given below.
You May Also Like:
PHP Age Calculator Program Source Code
Before sharing source code let’s talk about this program. First, I had created a form with many option bar for the day, month, and year. When user choose there DOB every option has their value. Using PHP $_POST
method this program fetches value and store in variables. After collecting User date of birth now I put date('d-m-Y');
to getting today’s date.
I said before creating this program using PHP is very easy because there is a PHP property called diff
( info ) for calculating the difference between DOB and current date. So let’s see the code.
For creating this program you have to create just 1 PHP file. This is a basic program, that’s why I had created it with one file and a simple look. Follow steps for creating this age calculator program without any error.
index.php
Create a PHP file named ‘index.php‘ and put these codes given here below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 |
<!DOCTYPE html> <!-- code by webdevtrick ( https://webdevtrick.com ) --> <html> <head> <title>PHP Age Calculator | Webdevtrick.com</title> <link href='http://fonts.googleapis.com/css?family=Roboto' rel='stylesheet' type='text/css'> <style> .calculate { width: 15%; background-color: #333; color: white; font-size: 18pt; border-radius: 15px; } form option { background-color: #333; color: white; } </style> </head> <body> <div align="center"> <br><br><br><br> <h3>Enter Your DOB</h3> <form action="index.php" method="post" class="drop"> <select name="day"> <option value="01">01</option> <option value="02">02</option> <option value="03">03</option> <option value="04">04</option> <option value="05">05</option> <option value="06">06</option> <option value="07">07</option> <option value="08">08</option> <option value="09">09</option> <option value="10">10</option> <option value="11">11</option> <option value="12">12</option> <option value="13">13</option> <option value="14">14</option> <option value="15">15</option> <option value="16">16</option> <option value="17">17</option> <option value="18">18</option> <option value="19">19</option> <option value="20">20</option> <option value="21">21</option> <option value="22" selected="selected">22</option> <option value="23">23</option> <option value="24">24</option> <option value="25">25</option> <option value="26">26</option> <option value="27">27</option> <option value="28">28</option> <option value="29">29</option> <option value="30">30</option> <option value="31">31</option> </select> <select name="month"> <option value="01">January</option> <option value="02">Febuary</option> <option value="03">March</option> <option value="04">April</option> <option value="05">May</option> <option value="06">June</option> <option value="07">July</option> <option value="08">August</option> <option value="09">September</option> <option value="10">October</option> <option value="11">November</option> <option value="12" selected="selected">December</option> </select> <select name="year"> <option value="2014">2014</option> <option value="2013">2013</option> <option value="2012">2012</option> <option value="2011">2011</option> <option value="2010">2010</option> <option value="2009">2009</option> <option value="2008">2008</option> <option value="2007">2007</option> <option value="2006">2006</option> <option value="2005">2005</option> <option value="2004">2004</option> <option value="2003">2003</option> <option value="2002">2002</option> <option value="2001">2001</option> <option value="2000" selected="selected">2000</option> <option value="1999">1999</option> <option value="1998">1998</option> <option value="1997">1997</option> <option value="1996">1996</option> <option value="1995">1995</option> <option value="1994">1994</option> <option value="1993">1993</option> <option value="1992">1992</option> <option value="1991">1991</option> <option value="1990">1990</option> <option value="1989">1989</option> <option value="1988">1988</option> <option value="1987">1987</option> <option value="1986">1986</option> <option value="1985">1985</option> <option value="1984">1984</option> <option value="1983">1983</option> <option value="1982">1982</option> <option value="1981">1981</option> <option value="1980">1980</option> <option value="1979">1979</option> <option value="1978">1978</option> <option value="1977">1977</option> <option value="1976">1976</option> <option value="1975">1975</option> <option value="1974">1974</option> <option value="1973">1973</option> <option value="1972">1972</option> <option value="1971">1971</option> <option value="1970">1970</option> <option value="1969">1969</option> <option value="1968">1968</option> <option value="1967">1967</option> <option value="1966">1966</option> <option value="1965">1965</option> <option value="1964">1964</option> <option value="1963">1963</option> <option value="1962">1962</option> <option value="1961">1961</option> <option value="1960">1960</option> <option value="1959">1959</option> <option value="1958">1958</option> <option value="1957">1957</option> <option value="1956">1956</option> <option value="1955">1955</option> <option value="1954">1954</option> <option value="1953">1953</option> <option value="1952">1952</option> <option value="1951">1951</option> <option value="1950">1950</option> <option value="1949">1949</option> <option value="1948">1948</option> <option value="1947">1947</option> <option value="1946">1946</option> <option value="1945">1945</option> <option value="1944">1944</option> <option value="1943">1943</option> <option value="1942">1942</option> <option value="1941">1941</option> <option value="1940">1940</option> <option value="1939">1939</option> <option value="1938">1938</option> <option value="1937">1937</option> <option value="1936">1936</option> <option value="1935">1935</option> <option value="1934">1934</option> <option value="1933">1933</option> <option value="1932">1932</option> <option value="1931">1931</option> <option value="1930">1930</option> </select> <br> <br> <input name="submit" type="submit" value="Calculate My Age!" class="calculate" /> </form> <?php if(isset($_POST['submit'])) { $d=$_POST['day']; $m=$_POST['month']; $y=$_POST['year']; $dob=$d.'-'.$m.'-'.$y; $bday=new DateTime($dob); $t=date('d-m-Y'); $age=$bday->diff(new DateTime); $today=date('d-m-Y'); echo '<br />'; echo '<b>Your Birth date: </b>'; echo $dob; echo '<br>'; echo '<b>Your Age : </b> '; echo $age->y; echo ' Years, '; echo $age->m; echo ' Months, '; echo $age->d; echo ' Days'; } ?> <br><br> </div> <div align="center"> Code By <a href="https://webdevtrick.com">Webdevtrick</a> </div> </body> </html> |
That’s It. Now you have successfully created a PHP age calculator program. Now you can modify this as you want. If you have any doubt or question comment down below.
Thanks For Visiting, Keep Visiting.