Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to get the age from a birthdate using PHP & MySQL?
    primarykey
    data
    text
    <p>I ask my users for their birthdate and store it in my database in the following way <code>$month</code> <code>$day</code> <code>$year</code> output <code>May 6 1901</code> but I was wondering how can I get the age from the stored birthdate using PHP &amp; MySQL?</p> <p>Here is the PHP code.</p> <pre><code>if (isset($_POST['submitted'])) { $mysqli = mysqli_connect("localhost", "root", "", "sitename"); $dbc = mysqli_query($mysqli,"SELECT users.* FROM users WHERE user_id=3"); $month_options = array("Month", "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"); $day_options = array("Day", "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"); $month = mysqli_real_escape_string($mysqli, htmlentities(strip_tags($_POST['month']))); $day = mysqli_real_escape_string($mysqli, htmlentities(strip_tags($_POST['day']))); $year = mysqli_real_escape_string($mysqli, htmlentities(strip_tags($_POST['year']))); if (mysqli_num_rows($dbc) == 0) { $mysqli = mysqli_connect("localhost", "root", "", "sitename"); $dbc = mysqli_query($mysqli,"INSERT INTO users (user_id, month, day, year) VALUES ('$user_id', '$month', '$day', '$year')"); } if ($dbc == TRUE) { $dbc = mysqli_query($mysqli,"UPDATE users SET month = '$month', day = '$day', year = '$year' WHERE user_id = '$user_id'"); echo '&lt;p class="changes-saved"&gt;Your changes have been saved!&lt;/p&gt;'; } if (!$dbc) { print mysqli_error($mysqli); return; } } </code></pre> <p>Here is the html.</p> <pre><code>&lt;form method="post" action="index.php"&gt; &lt;fieldset&gt; &lt;ul&gt; &lt;li&gt;&lt;label&gt;Date of Birth: &lt;/label&gt; &lt;label for="month" class="hide"&gt;Month: &lt;/label&gt; &lt;?php // month options echo '&lt;select name="month" id="month"&gt;' . "\n"; foreach($month_options as $option) { if ($option == $month) { echo '&lt;option value="' . stripslashes(htmlentities(strip_tags($option))) . '" selected="selected"&gt;' . stripslashes(htmlentities(strip_tags($option))) . '&lt;/option&gt;' . "\n"; } else { echo '&lt;option value="'. stripslashes(htmlentities(strip_tags($option))) . '"&gt;' . stripslashes(htmlentities(strip_tags($option))) . '&lt;/option&gt;'."\n"; } } echo '&lt;/select&gt;'; ?&gt; &lt;label for="day" class="hide"&gt;Day: &lt;/label&gt; &lt;?php // day options echo '&lt;select id="day" name="day"&gt;' . "\n"; foreach($day_options as $option) { if ($option == $day) { echo '&lt;option value="' . stripslashes(htmlentities(strip_tags($option))) . '" selected="selected"&gt;' . stripslashes(htmlentities(strip_tags($option))) . '&lt;/option&gt;' . "\n"; } else { echo '&lt;option value="'. stripslashes(htmlentities(strip_tags($option))) . '"&gt;' . stripslashes(htmlentities(strip_tags($option))) . '&lt;/option&gt;'."\n"; } } echo '&lt;/select&gt;'; ?&gt; &lt;label for="year" class="hide"&gt;Year: &lt;/label&gt;&lt;input type="text" name="year" id="year" size="4" maxlength="4" value="&lt;?php if (isset($_POST['year'])) { echo stripslashes(htmlentities(strip_tags($_POST['year']))); } else if(!empty($year)) { echo stripslashes(htmlentities(strip_tags($year))); } ?&gt;" /&gt;&lt;/li&gt; &lt;li&gt;&lt;input type="submit" name="submit" value="Save Changes" class="save-button" /&gt; &lt;input type="hidden" name="submitted" value="true" /&gt; &lt;input type="submit" name="submit" value="Preview Changes" class="preview-changes-button" /&gt;&lt;/li&gt; &lt;/ul&gt; &lt;/fieldset&gt; &lt;/form&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload