title: php calculate age from birth date html form
body: <p>I am looking for a way to calculate the age when the user enter his birth date in html form and press the submit button. I saw similar questions but they all about calculating already existing age but here I am talking about entering date and then calculating the age.</p>
  
  <p>I tried to make the code but it gives me wrong age:</p>
  
  <pre><code>&lt;form action="test.php" method="post"&gt;   
  
  Enter your date of birth:
  &lt;select name="month" &gt;
  &lt;option value="1"&gt;January&lt;/option&gt;
  &lt;option value="2"&gt;February&lt;/option&gt;
  &lt;option value="3"&gt;March&lt;/option&gt;
  
  &lt;/select&gt;
  &lt;select name="day" id="day"&gt;
  &lt;option value="1"&gt;1&lt;/option&gt;
  &lt;option value="2"&gt;2&lt;/option&gt;
  &lt;option value="3"&gt;3&lt;/option&gt;
  &lt;/select&gt;
  &lt;select name="year" id="year"&gt;
  &lt;option value="1"&gt;2013&lt;/option&gt;
  &lt;option value="2"&gt;2012&lt;/option&gt;
  &lt;option value="3"&gt;2011&lt;/option&gt;
  &lt;/select&gt;
  &lt;input type="submit" name="submit" value="age" &gt;
  
  
  &lt;?php
  if (isset($_POST['submit']))
  {
  $year = $_POST['year'];
  $month = $_POST['month'];
  $day = $_POST['day'];
  if ($year != '' &amp;&amp; $month != '' &amp;&amp; $day != '') {
  $birthDate = $year.'-'.$month.'-'.$day;
  
  $age = (date("md", date("U", mktime(0, 0, 0, $birthDate[2], $birthDate[1],  $birthDate[0]))) &gt; date("md") ? ((date("Y")-$birthDate[0])-1):(date("Y")-$birthDate[0]));
  echo "Age is: ".$age;
  }}
  ?&gt;
  </code></pre>