Hey guys I'm I need get a user's date of birth in a form...How do you think I should go about doing it? I would like to just keep it simple in a text field but I also need to validate it.
After that I need to store it in my database, the database has a field which is a timestamp is that correct or should I use something different? (MYSQL)
I have it working at the moment but it also shows the time as 00开发者_Go百科:00:00 in the database.
I'm using this
$DOB = strtotime($DOB);
How should I do it? Is there a better way? This requires them to enter dob as YYYY/MM/DD is there a way I can do it better...
After this I also need to make sure they are over 18 years old. So how should I go about doing that?
To get a user to input a date, you could use something like jQuery's Datepicker : it'll allow your users to input a date typing it, or selecting it using some calendar widget.
To store a date in a MySQL database, you should use a DATE
field -- and not :
- A
DATETIME
, which includes a time - Nor a
TIMESTAMP
, which includes a time, and is limited to 1970-2038 (which doesn't feel quite right for a date of birth)
In order to use strtotime()
, your dates should look like YYYY-MM-DD
(like in the database, when using a DATE
field), and not DD/MM/YYYY
nor MM/DD/YYYY
(those can be confusing)
In PHP, you should also use the DateTime
class, and not UNIX Timestamps : those are limited to 1970-2038 (on 32 bits platforms).
convert it like this date('Y-m-d',strtotime($DOB))
In your client side script or HTML file:
<form action="myphp.php">
Day
<select size="1" name="day">
<option value="01" label="1st">1st</option>
<option value="02" label="2nd">2nd</option>
...
</select>
Month
<select size="1" name="month">
<option value="01" label="Jan">Jan</option>
<option value="02" label="Feb">Feb</option>
...
</select>
Year <select size="1" name="year">
...
<option value="1979" label="1979">1979</option>
<option value="1980" label="1980">1980</option>
...
</select>
</form>
In you .php back end:
$DOB = $_REQUEST['year']."/".$_REQUEST['month'].$_REQUEST['day'];
Now $DOB
is in a usable format, you can validate the $_REQUEST[...]
how ever you like. But I suggest using JQuery on the HTML page as well as server side validation.
You should use fieldtype "DATE", which would only store the date-part of it. Otherwise, you can use select DATE(fieldname) ... when selecting it.
To validate the users age, you can use DATE_SUB to calculate the age from todays date and then subtracting the users birthday.
first of all you can't use timestampt to store birthdays because timestamp start counting from 1970 and there are some ppl in the world over 40, therefor use date instead (not datetime because you don't need to store time, just birth date)
you can get the date from the user via simple text field, but you should notify him about the expected date format (d/m/y, m/d/y ,...) 2/4/2011 can stand for February 4 2011 or April 2 2011
after you know what you except you check the input with regex
for example, $_POST['birthday'] = '11/3/2011' and we except the format m/d/y
$birthday = $_POST['birthday'];
if(preg_match('/^(\d){1,2}\/(\d){1,2}\/(\d){4}$/',$birthday)){
list($month,$day,$year) = explode('/',$birthday);
// here you should check that $day isn't bigger then days_in_month($month), and $month is between 1-12
if(date('Y')-$year<18){ // you need to take into account $month to be more strict
// user under 18
}
}
hope it helped
精彩评论