User age within database

Linkz0rs

Member
Messages
247
Reaction score
7
Points
18
Hello, I have a very simple question.
Could anyone help me out with trying to grab a users age from a database and properly outputting into declarations.
I'm trying to code my own profiling system for a website, and I'm a little confused on this process.

I already have the code to fetch the proper field in the database where it contains the users age, but what I cannot quite get is for it to then take the age from database in the format of "M-D-YYYY" into other declarations I have assigned:
Code:
$DOBmonth (the month)
$DOBday (the day)
$DOByear (the year)
And let me be very straight forward here, those are the NAMES that will be used for POST and such.
I want this to output in the said fields so if the user wishes to modify their DOB (or inputting their age upon registration), it will output it in the proper <select> field's I've created.

Here's what the <select> field looks like so you get the gist:
Code:
<select name='bdaymonth'>
<option value=''>- Month -</option>
<option value='Jan'>January</option>
<option value='Feb'>February</option>
<option value='March'>March</option>
<option value='April'>April</option>
<option value='May'>May</option>
<option value='June'>June</option>
<option value='July'>July</option>
<option value='Aug'>August</option>
<option value='Sep'>September</option>
<option value='Oct'>October</option>
<option value='Nov'>November</option>
<option value='Dec'>December</option>
</select>
<select name='bdayday'>
<option value=''>- Day -</option>
<option value='1'>1</option>
<option value='2'>2</option>
<option value='3'>3</option>
<option value='4'>4</option>
<option value='5'>5</option>
<option value='6'>6</option>
<option value='7'>7</option>
<option value='8'>8</option>
<option value='9'>9</option>
<option value='10'>10</option>
<option value='11'>11</option>
<option value='12'>12</option>
<option value='13'>13</option>
<option value='14'>14</option>
<option value='15'>15</option>
<option value='16'>16</option>
<option value='17'>17</option>
<option value='18'>18</option>
<option value='19'>19</option>
<option value='20'>20</option>
<option value='21'>21</option>
<option value='22'>22</option>
<option value='23'>23</option>
<option value='24'>24</option>
<option value='25'>25</option>
<option value='26'>26</option>
<option value='27'>27</option>
<option value='28'>28</option>
<option value='29'>29</option>
<option value='30'>30</option>
<option value='31'>31</option>
</select>
<select name='bdayyear'>
<option value=''>- Year -</option>
<option value='2000'>2000</option>
<option value='2001'>2001</option>
<option value='2002'>2002</option>
<option value='2003'>2003</option>
<option value='2004'>2004</option>
<option value='2005'>2005</option>
</select>
It would be preferred that the user cannot pick the default choice (labeled "- Month -" for example), nor can the user pick a non-realistic day (for example, only 28 days in Feb.)

Like how my B-Day is May, 19...
In the database, it would display as "May-19-1989".

Also, on the profile, I want it to also determine the age and outputting it, for example: "You are {age} years old".

I'm accomplishing this through ONE page (I do NOT want multiple pages for this, I'd prefer to do everything in just that one page and not making it take the user to another page [Like I've seen stuff like user login/register systems do]... ONE page, I already know how to make it read from the same page and process everything properly, I'm just confused on this one minor bit. Someone posting the code to accomplish such a task would be very appreciative.
 
Last edited:

misson

Community Paragon
Community Support
Messages
2,572
Reaction score
72
Points
48
To make an option unselectable after a user has selected another option, give it the "disabled" attribute. To ensure a user picks a value for each input and that the date is valid, use client-side and server-side form validation. Client-side validation should be done as early as possible. For selects, this is when the form is submitted. For overall date validation, this is when all inputs have a value and they all lose focus. Validation failure messages should be shown in-line, next to the element that failed (don't use alerts). JS library plugins (such as the jQuery Validation Plugin) make this dead simple.

However, instead of using three inputs for the date, use a single input with masking and a date picker (such as jQuery UI's date picker), which is a more usable design. Use strftime or date to format the current DOB in the database when setting the default value of the input.

For date columns, MySQL only supports formats of "YYYY-M-D" (as a string) and "YYYYMMDD" (as a string or number), since date and lexical orderings are then the same.

To get the number of years between two dates (the DoB & now), use DateTime->diff/date_diff. This is perhaps best handled by having a User class with and age method.

I can't tell if there are any other issues with which you're having problems.
 

Linkz0rs

Member
Messages
247
Reaction score
7
Points
18
.. was looking towards an example set of code that can set me in the right path.. but.. thanks, i guess? :/
 
Last edited:

misson

Community Paragon
Community Support
Messages
2,572
Reaction score
72
Points
48
Code samples for what? Except for creating a class to handle users (which is too dependent on other aspects of the system for me to even begin to think about implementaiton), everything that I mentioned is trivial, or uses a separate library, or both. So, you're welcome, I guess.
 
Last edited:
Top