How to get the age from a birthdate using PHP & MySQL?
- by TaG
I ask my users for their birthdate and store it in my database in the following way $month $day $year output May 6 1901 but I was wondering how can I get the age from the stored birthdate using PHP & MySQL?
Here is the PHP 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 '<p class="changes-saved">Your changes have been saved!</p>';
}
if (!$dbc) {
print mysqli_error($mysqli);
return;
}
}
Here is the html.
<form method="post" action="index.php">
<fieldset>
<ul>
<li><label>Date of Birth: </label>
<label for="month" class="hide">Month: </label>
<?php // month options
echo '<select name="month" id="month">' . "\n";
foreach($month_options as $option) {
if ($option == $month) {
echo '<option value="' . stripslashes(htmlentities(strip_tags($option))) . '" selected="selected">' . stripslashes(htmlentities(strip_tags($option))) . '</option>' . "\n";
} else {
echo '<option value="'. stripslashes(htmlentities(strip_tags($option))) . '">' . stripslashes(htmlentities(strip_tags($option))) . '</option>'."\n";
}
}
echo '</select>';
?>
<label for="day" class="hide">Day: </label>
<?php // day options
echo '<select id="day" name="day">' . "\n";
foreach($day_options as $option) {
if ($option == $day) {
echo '<option value="' . stripslashes(htmlentities(strip_tags($option))) . '" selected="selected">' . stripslashes(htmlentities(strip_tags($option))) . '</option>' . "\n";
} else {
echo '<option value="'. stripslashes(htmlentities(strip_tags($option))) . '">' . stripslashes(htmlentities(strip_tags($option))) . '</option>'."\n";
}
}
echo '</select>';
?>
<label for="year" class="hide">Year: </label><input type="text" name="year" id="year" size="4" maxlength="4" value="<?php if (isset($_POST['year'])) { echo stripslashes(htmlentities(strip_tags($_POST['year']))); } else if(!empty($year)) { echo stripslashes(htmlentities(strip_tags($year))); } ?>" /></li>
<li><input type="submit" name="submit" value="Save Changes" class="save-button" />
<input type="hidden" name="submitted" value="true" />
<input type="submit" name="submit" value="Preview Changes" class="preview-changes-button" /></li>
</ul>
</fieldset>
</form>