Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 10 of 10
  1. #1
    Regular Coder
    Join Date
    Mar 2005
    Posts
    240
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Storing Date of Birth

    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

  • #2
    Mega-ultimate member
    Join Date
    Jun 2002
    Location
    Winona, MN - The land of 10,000 lakes
    Posts
    1,855
    Thanks
    1
    Thanked 45 Times in 42 Posts
    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).

  • #3
    Regular Coder
    Join Date
    Mar 2005
    Posts
    240
    Thanks
    1
    Thanked 0 Times in 0 Posts
    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

  • #4
    fci
    fci is offline
    Senior Coder
    Join Date
    Aug 2004
    Location
    Twin Cities
    Posts
    1,345
    Thanks
    0
    Thanked 0 Times in 0 Posts
    what do you plan on using the dob for?

  • #5
    Regular Coder
    Join Date
    Mar 2005
    Posts
    240
    Thanks
    1
    Thanked 0 Times in 0 Posts
    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?

  • #6
    fci
    fci is offline
    Senior Coder
    Join Date
    Aug 2004
    Location
    Twin Cities
    Posts
    1,345
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by losse
    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.

  • #7
    Regular Coder
    Join Date
    Sep 2005
    Posts
    394
    Thanks
    1
    Thanked 0 Times in 0 Posts
    In terms of storage space, you'd be better storing it as a unix timestamp in an unsigned integer field.

    ~Phil~

  • #8
    Regular Coder
    Join Date
    May 2004
    Posts
    144
    Thanks
    0
    Thanked 0 Times in 0 Posts
    if it is just to display a dob, you could just concat the 3 form values together.:

    PHP Code:
    $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
    Last edited by ClubCosmic; 06-13-2006 at 01:49 AM.

  • #9
    Regular Coder
    Join Date
    Jun 2005
    Posts
    804
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  • #10
    Super Moderator guelphdad's Avatar
    Join Date
    Mar 2006
    Location
    St. Catharines, Ontario Canada
    Posts
    2,634
    Thanks
    4
    Thanked 148 Times in 139 Posts
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •