View Full Version : Storing Date of Birth

06-12-2006, 06:19 PM
hi there
What's the best way to store a date of birth in a database when you have an HTML form with 3 drop down menu for "day" "month and "year"

would you create three fields in the database one for day, month and year or would you store it as one field showing, for example, 10 June 1980?


06-12-2006, 06:24 PM
I'd recommend storing them as one field in the database as either an int datatype or a date / datetime datatype. I'm not going to go into the merits or date / datetime vs. int, but those are probably the two most common things to do. If you go with int, you'll need to convert your input into a timestamp to store it in the database (using strtotime or something similar).

06-12-2006, 06:32 PM
I've never used that before?

Would you mind letting me know how I would create the field in PHPMyAdmin and what the quiery would be in PHP to send that to the DB?


06-12-2006, 06:46 PM
what do you plan on using the dob for?

06-12-2006, 07:27 PM
It's a client input on a form... Basically to store it and perhaps later display it in a CMS site.

Does that answer your question?

06-12-2006, 07:36 PM
It's a client input on a form... Basically to store it and perhaps later display it in a CMS site.

Does that answer your question?

if you use a date column, when you select it you will either need to do:
select date_format(dob, $formating) as dob from table
or to convert it to a unix timestamp:
select from_unixtime(dob, $formating) as dob from table

if you use an int column you will do something like this
select dob from table
then use php to format it
date($formating, $row['dob'])

I think you should use a date column.

06-12-2006, 11:10 PM
In terms of storage space, you'd be better storing it as a unix timestamp in an unsigned integer field.


06-13-2006, 12:45 AM
if it is just to display a dob, you could just concat the 3 form values together.:

$dob = $_POST[day] . " " . $_POST[month] " " . $_POST[year];

which could display something like 10 May 1975
then you could insert the variable $dob into your database as a varchar type.

quick and easy.
good luck

Kid Charming
06-13-2006, 01:05 AM
I would recommend a date type; MySQL's built-in date and time functions will give you the widest range of options for manipulating/comparing your dates. Even if you're only displaying the date now, you may decide you want to use that date for more complex purposes later on.

06-13-2006, 01:32 AM
If you are storing birthdates and use UNIXTIME in mysql you are limited to dates after Jan 1 1970. Use datetype (it can hold values between 1000 and 9999 A.D.). since you aren't using any time values you won't need to use datetime.

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum