Calculate Age From Date of Birth using PHP

This simple tutorial, the user can learn on how to calculate their age based on their date of birth using PHP. Using three select elements, which is the Day, Month, and Year of their birth date. Creating three selecting elements for the Day, Month, and Year to select the user their date of birth as shown in the image below. Result Here's the individual source code for the Day, Month, and Year.

Day

  1. <select name="day" required>
  2.         <option value="">Day</option>
  3.         <?php
  4.         for($day=01;$day<=31;$day++)
  5.         {
  6.         echo "<option value='$day'>$day</option>";
  7.         }
  8.         ?>
Result for the Day source code. Result

Month

  1. <select name="month" class="month_style" required>
  2.         <option value="" selected>Month</option>
  3.         <option value="1">January</option>
  4.         <option value="2">February</option>
  5.         <option value="3">March</option>
  6.         <option value="4">April</option>
  7.         <option value="5">May</option>
  8.         <option value="6">June</option>
  9.         <option value="7">July</option>
  10.         <option value="8">August</option>
  11.         <option value="9">September</option>
  12.         <option value="10">October</option>
  13.         <option value="11">November</option>
  14.         <option value="12">December</option>
Result for the Month source code. Result

Year

  1. <select name="year" required>
  2.         <option value="">Year</option>
  3.         <?php
  4.         for($year=date('Y');$year>=1950;$year--)
  5.         {
  6.         echo "<option value='$year'>$year</option>";
  7.         }
  8.         ?>
Result for the Year source code. Result This simple project can be very useful. Hope this project will help you. Share us your thoughts and comments below. Thank you so much for dropping by and reading this tutorial post. For more updates, don’t hesitate and feel free to visit this website more often and please share this with your friends or email me at [email protected]. Practice Coding. Thank you very much.

Add new comment