...

View Full Version : Storing a users date of birth in mysql DATE field question



karlosio
10-10-2009, 11:34 PM
Hi I am creating a registration form for my site that contains three fields for the date of birth which I want to store in a mysql database. I would like to append the entire date of birth values to a single date field in mysql (YYYY-MM-DD format) so I can do date calculations such as age. How could I do this or would it better to store each field value in a separate database table column. Heres my code for my form so far.



<form>
<!-- previous elements -->
<select name="dobd">
<option value="">Day</option>
<?php
for($i = 1; $i <=31; $i++)
{
echo '<option value="'.$i.'">'.date('d', mktime(0,0,0,0,$i,0)).'</option>\n';
}
?>
</select>
<select name="dobm">
<option value="">Month</option>
<?php
for($i = 1; $i <= 12; $i++)
{
echo '<option value="'.$i.'">'.date('F', mktime(0,0,0,$i+1,0,0)).'</option>\n';
}
?>
</select>
<input type="text" name="doby" size="4" maxlength="4" />
<!-- later elements -->
</form>


Also as you can see I'm trying to create the days and month options using for loops. But my problem is I would like the form to remember these values on user error. Ive tried entering:



($_POST['dobd'] == $i) ? 'selected="selected"' : ''


The for loop is a coding time-saver however putting that conditional in the loop does not work. Sorry I'm a PHP novice. Thanks in advance.

Eran
10-11-2009, 02:44 AM
1. you should save day, month and year to unix timepost.
when it's a one field unix timepost you can play with that as much as you want.
http://php.net/manual/en/function.date.php and mkdir ..

2. the same as you did


<select name="dobm">
<option value="">Month</option>
<?php
for($i = 1; $i <= 12; $i++)
{
if ($_POST['dobd'] == $i)
echo '<option selected="selected" value="'.$i.'">'.date('F', mktime(0,0,0,$i+1,0,0)).'</option>\n';
else
echo '<option value="'.$i.'">'.date('F', mktime(0,0,0,$i+1,0,0)).'</option>\n';
}
?>
</select>

karlosio
10-11-2009, 02:12 PM
1. you should save day, month and year to unix timepost.
when it's a one field unix timepost you can play with that as much as you want.
http://php.net/manual/en/function.date.php and mkdir ..

2. the same as you did


<select name="dobm">
<option value="">Month</option>
<?php
for($i = 1; $i <= 12; $i++)
{
if ($_POST['dobd'] == $i)
echo '<option selected="selected" value="'.$i.'">'.date('F', mktime(0,0,0,$i+1,0,0)).'</option>\n';
else
echo '<option value="'.$i.'">'.date('F', mktime(0,0,0,$i+1,0,0)).'</option>\n';
}
?>
</select>


Thank you! :thumbsup:



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum