...

View Full Version : Storing Date of Birth



losse
06-12-2006, 07: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?

Thanks

bcarl314
06-12-2006, 07: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).

losse
06-12-2006, 07: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?

Thanks

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

losse
06-12-2006, 08: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?

fci
06-12-2006, 08: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.

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

~Phil~

ClubCosmic
06-13-2006, 01: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, 02: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.

guelphdad
06-13-2006, 02: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