Web Application: Age Calculator in PHP with source code, demo, and also how you can execute full program easily. To complete the program you need to follow those steps given bellow
Hi coder, previously we tried to COVID-19 Tracker application with PHP. Now its time to calculate age. When I start coding this the one program that I like so much. Lots of PHP programs and calculation parts are coming in an upcoming post so stay with us and don’t forget to bookmark our website.
Live Demo
Also Helpful –
PHP Age Calculator Source Code
To complete this program with debugging free code you have to create only one file(index.php) because we use the bootstrap framework to style our CSS. And we also post data on the same page using the PHP function "isset($_POST['submit']))"
that only show data ones it’s submitted.
Next, Here we two types of date “Date of Birth” and “Age at the Date of “. When you select both the date and submit for the result then the PHP function will execute and display the result of your selected date of birth. It’s predominantly used DateTime()
and date diff()
function. DateTime()
function used to set date in DateTime format and diff()
function used for differentiating between two dates.
Back to coding,
index.php
Create a PHP file named ‘index.php‘ and put those codes given 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 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 |
<!-- Coding Debugging (https://codingdebugging.com) --> <!DOCTYPE html> <html lang="en"> <head> <title>Age Calculator - Coding Debugging</title> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <!-- Bootstrap CSS --> <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css"> <!-- Bootstrap JS --> <script src="https://code.jquery.com/jquery-3.4.1.slim.min.js"></script> <script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js"></script> <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js"></script> <!-- Font Awesome --> <script src="https://kit.fontawesome.com/996973c893.js" crossorigin="anonymous"></script> <!-- Google Fonts --> <link href="https://fonts.googleapis.com/css2?family=Baloo+Thambi+2:wght@400;500;600;700;800&display=swap" rel="stylesheet"> <!-- My Stylesheet --> <style type="text/css"> body { font-family: "Baloo Thambi 2", cursive; } </style> </head> <body> <div class="container-fluid bg-light p-5 text-center my-3"> <h2 class="">Age Calculator</h1> <h5 class="text-muted">CALCULATE YOUR AGE WITH YEARS, MONTHS, DAYS, WEEKS, DAYS, HOURS, MINUTES, SECONDS & SOME INTERESTING FACTS.</h5> </div> <div class="container bg-light p-3 my-5 text-center"> <div class="card-body"> <form action="index.php" method="post" class="form-group"> <div class="row" align="left"> <div class="col-md-12"> <button class="btn btn-dark">Date of Birth</button> </div> </div> <br> <div class="row mb-3"> <div class="col-md-4"> <select name="day" class="form-control"> <?php for($i=1;$i<=31;$i++) { ?> <option value="<?php echo $i;?>" <?php if($i=='1'){echo "selected";}?>><?php echo $i;?></option> <?php } ?> </select> </div> <div class="col-md-4"> <select name="month" class="form-control"> <?php for($i=1;$i<=12;$i++) { $month = date('F', mktime(0,0,0,$i, 1, date('Y'))); ?> <option value="<?php echo $i;?>" <?php if($i=='5'){echo "selected";}?>><?php echo $month;?></option> <?php } ?> </select> </div> <div class="col-md-4"> <select name="year" class="form-control"> <?php $year = date('Y'); for($i=1900;$i<=$year;$i++) { ?> <option value="<?php echo $i;?>"<?php if($i=='1997'){echo "selected";}?>><?php echo $i;?></option> <?php } ?> </select> </div> </div> <div class="row" align="left"> <div class="col-md-12"> <button class="btn btn-dark">Age at the Date of</button> </div> </div> <br> <div class="row mb-3"> <div class="col-md-4"> <select name="current_day" class="form-control"> <?php for($i=1;$i<=31;$i++) { ?> <option value="<?php echo $i;?>" <?php if($i==date('d')){echo "selected";}?>><?php echo $i;?></option> <?php } ?> </select> </div> <div class="col-md-4"> <select name="current_month" class="form-control"> <?php for($i=1;$i<=12;$i++) { $month = date('F', mktime(0,0,0,$i, 1, date('Y'))); ?> <option value="<?php echo $i;?>"<?php if($i==date('m')){echo "selected";}?>><?php echo $month;?></option> <?php } ?> </select> </div> <div class="col-md-4"> <select name="current_year" class="form-control"> <?php $year = date('Y'); for($i=1900;$i<=$year;$i++) { ?> <option value="<?php echo $i;?>" <?php if($i==date('Y')){echo "selected";}?>><?php echo $i;?></option> <?php } ?> </select> </div> </div> <div class="row" align="right"> <div class="col-md-12"> <input type="submit" name="submit" class="btn btn-primary " value="Calculate Age"> </div> </div> </form> </div> <div class="card-footer" align="left"> <?php if(isset($_POST['submit'])) { ?> <div class="row" align="left"> <div class="col-md-12"> <button class="btn btn-dark">Result</button> </div> </div> <br> <?php $hours_in_day = 24; $minutes_in_hour= 60; $seconds_in_mins= 60; $dob=$_POST['day'].'-'.$_POST['month'].'-'.$_POST['year']; $current_dob=$_POST['current_day'].'-'.$_POST['current_month'].'-'.$_POST['current_year']; $birth_date = new DateTime($dob); $current_date = new DateTime($current_dob); $diff = $birth_date->diff($current_date); echo $years = $diff->y . " years " . $diff->m . " months " . $diff->d . " days"; echo "<br/>"; echo $months = ($diff->y * 12) + $diff->m . " months " . $diff->d . " days"; echo "<br/>"; echo $weeks = floor($diff->days/7) . " weeks " . $diff->d%7 . " days"; echo "<br/>"; echo $days = $diff->days . " days"; echo "<br/>"; echo $hours = $diff->h + ($diff->days * $hours_in_day) . " hours"; echo "<br/>"; echo $mins = $diff->h + ($diff->days * $hours_in_day * $minutes_in_hour) . " minutes"; echo "<br/>"; echo $seconds = $diff->h + ($diff->days * $hours_in_day * $minutes_in_hour * $seconds_in_mins) . " seconds"; echo "<br/>"; ?> <br> <div class="row" align="left"> <div class="col-md-12"> <button class="btn btn-dark">Facts</button> </div> </div> <br> <?php echo $a = (3 * preg_replace('/[^0-9]/', '', $days)) . " breaths liters of oxygen"; echo "<br/>"; echo $b = (5 * preg_replace('/[^0-9]/', '', $mins)) . " times heart beaten"; echo "<br/>"; echo $c = (1 * preg_replace('/[^0-9]/', '', $years)) . " tons of food"; echo "<br/>"; echo $d = (10 * preg_replace('/[^0-9]/', '', $days)) . " times laughed"; echo "<br/>"; echo $e = (5 * preg_replace('/[^0-9]/', '', $months)) . " centimeter hairs growth"; echo "<br/>"; } ?> </div> </div> <hr> <footer class="footer mt-auto py-3 bg-light"> <div class="container text-center"> <span class="text-muted">Copyright ©2020 | Age Calculator | <a href="https://codingdebugging.com/" target="_blank">CodingDebugging.Com</a></span> </div> </footer> </body> </html> |
Thank you for visiting!
We appreciate you for your precious time. I hope you are learn something from here! If you have any queries about this coding comment down below we will reply ASAP.
Keep coding, Keep debugging!