Jump to content

storing dob into database


runnerjp

Recommended Posts

i have got the code as follows for a user to select their date of birth

 

<?php  
   $months = array (1 => 'January', 'February', 'March', 'April', 'May', 'June','July', 'August', 'September', 'October', 'November', 'December');
$weekday = array('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday');
$days = range (1, 31);
$years = range (1910, 2015);

//**********************************************

echo "Day: <select name='day'>";
foreach ($days as $value) {
   echo '<option value="'.$value.'">'.$value.'</option>\n';
} echo '</select>';

echo "Month: <select name='month'>";
foreach ($months as $value) {
   echo '<option value="'.$value.'">'.$value.'</option>\n';
} echo '</select>';

echo "Year: <select name='year'>";
foreach ($years as $value) {
   echo '<option value="'.$value.'">'.$value.'</option>\n';
} 
?>

 

but what would be the method in which i stored it into my db?  would i have to join it ??  i was thinkin of storing it as "date" rather then "int"

Link to comment
Share on other sites

I've always used INT for storing all my dates and times and after a little persuasion have started to play with DATETIME and DATE.

 

Definitely worth taking a look at.

 

MySQL has some very handy functions for handling dates so why not pass as much date handling onto that instead of PHP doing all the work?

Link to comment
Share on other sites

ok guys so would i just dooo

 

$birthyear =  mysql_real_escape_string( $_POST['year']);

$birthmonth =  mysql_real_escape_string( $_POST['month']);

$birthday =  mysql_real_escape_string( $_POST['day']);

$dob = $birthday.'-'.$birthmonth.'-'.$birthyear;

 

and stor it like that?

Link to comment
Share on other sites

Your code could still product an error

 

assume someone would have these values posted

$birthyear ="some string";
$birthmonth ="some other string";
$birthday = "and yet another one";

that would result in

$dob="some string-some other string-and yet another one";

once you insert or update that into the db that would produce an error if the field type would be a date or timestamp

 

you could parse them all to integers or you could use a regex to validate if it is a valid date

Link to comment
Share on other sites

A Unix timestamp is not usable for birthdays because of the 1970 limitation (depending on operating system and php version.)

 

After you validate the entered date, form a yyyy-mm-dd DATE from the individual pieces and store it in a DATE data type in the database.

Link to comment
Share on other sites

ok so why not store the date in int as say 1984-09-04 by just doing

 

<?php $birthyear =  mysql_real_escape_string( $_POST['year']);
$birthmonth =  mysql_real_escape_string( $_POST['month']);
$birthday =  mysql_real_escape_string( $_POST['day']);
$dob = $birthday.'-'.$birthmonth.'-'.$birthyear; ?>

storing $dob into table...

 

then i could work out the age like so...

 


<?php
$dob = '1984-09-04';
$age = date('Y') - date('Y', strtotime($dob));
if (date('md') < date('md', strtotime($dob))) {
    $age--;
}
?>

Link to comment
Share on other sites

ok so if i have the following form and the values already filled in

<form>
  <input type="text" name="year" value="string" />
  <input type="text" name="month" value="string" />
  <input type="text" name="day"" value="string" />
  <input type="submit" name="submit" value="submit" />
</form>

then echo out your $dob when you submited the form

it would give you

echo $dob;//gives string-string-string

 

why not do something like this

 

if(checkdate($_POST['month'],$_POST['day'], $_POST['year'])){
  $dob = $birthyear.'-'.$birthmonth.'-'.$birthday;
}else{
  //not a valid input action
}

Link to comment
Share on other sites

ok i attempted it and got this as an error

 

Warning: checkdate() expects parameter 2 to be long, string given in /home/runningp/public_html/members/include/update.php on line 68

 

<?php  
$months = array (1 => 'January', 'February', 'March', 'April', 'May', 'June','July', 'August', 'September', 'October', 'November', 'December');
$weekday = array('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday');
$days = range (1, 31);
$years = range (1910, 2015);

//**********************************************

echo "Day: <select name='day'>";
foreach ($days as $value) {
echo '<option value="'.$value.'">'.$value.'</option>\n';
} echo '</select>';

echo "Month: <select name='month'>";
foreach ($months as $value) {
echo '<option value="'.$value.'">'.$value.'</option>\n';
} echo '</select>';

echo "Year: <select name='year'>";
foreach ($years as $value) {
echo '<option value="'.$value.'">'.$value.'</option>\n';
} 

$year =  mysql_real_escape_string( $_POST['year']);
$month =  mysql_real_escape_string( $_POST['month']);
$day =  mysql_real_escape_string( $_POST['day']);
$dob = $day.'-'.$month.'-'.$year;
if(checkdate($_POST['day'],$_POST['month'], $_POST['year'])){
  $dob = $birthday.'-'.$birthmonth.'-'.$birthyear;
}else{
echo 'error!';
}
?>

Link to comment
Share on other sites

i don't use make time, i put it together into a timestamps knowing the order of a timestamps then i insert it into the database, if you have mysql 4 it will automatically put - in for you.

 

<?php
if($users_expires_day && $users_expires_month && $users_expires_year) $this_users->users_expires = mktime($h, $m, $s, $users_expires_month, $users_expires_day, $users_expires_year);
?>

 

ok i lied i do use maketime, somtimes

 

$this_users->users_expires is now a timestamp, if you insert it into mysql 5 it will add the dashes and colons etc so on re reading it from teh databse you will need to work with these, ereg replace them dashes and then substring get the -4, 4 = year -6, 2 = day -8, 2 = month etc etc.

Link to comment
Share on other sites

ok guys so would i just do

 

$birthyear =  mysql_real_escape_string( $_POST['year']);

$birthmonth =  mysql_real_escape_string( $_POST['month']);

$birthday =  mysql_real_escape_string( $_POST['day']);

$dob = $birthday.'-'.$birthmonth.'-'.$birthyear;

 

and store it like that?

 

you need the h,s,m ,

 

$dob = "000000".$birthday.$birthmonth.$birthyear;

 

mysql 5 will add teh dashes for you

Link to comment
Share on other sites

ok so if i have the following form and the values already filled in

<form>
  <input type="text" name="year" value="string" />
  <input type="text" name="month" value="string" />
  <input type="text" name="day"" value="string" />
  <input type="submit" name="submit" value="submit" />
</form>

then echo out your $dob when you submited the form

it would give you

echo $dob;//gives string-string-string

 

why not do something like this

 

if(checkdate($_POST['month'],$_POST['day'], $_POST['year'])){
  $dob = $birthyear.'-'.$birthmonth.'-'.$birthday;
}else{
  //not a valid input action
}

 

i had alot of issues with this you dont need to add the dashes when its going in, i work with mysql 4 and 5 so i had a major issue because mysql 4 dosent store it with dashes.

Link to comment
Share on other sites

ok i stored it into the db liek this

 

i used

<?php
$year =  mysql_real_escape_string( $_POST['year']);
$month =  mysql_real_escape_string( $_POST['month']);
$day =  mysql_real_escape_string( $_POST['day']);
$dob = $day.'-'.$month.'-'.$year; 

$months = array (1 => 'January', 'February', 'March', 'April', 'May', 'June','July', 'August', 'September', 'October', 'November', 'December');
$weekday = array('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday');
$days = range (1, 31);
$years = range (1910, 2015);

//**********************************************

echo "Day: <select name='day'>";
foreach ($days as $value) {
echo '<option value="'.$value.'">'.$value.'</option>\n';
} echo '</select>';

echo "Month: <select name='month'>";
foreach ($months as $value) {
echo '<option value="'.$value.'">'.$value.'</option>\n';
} echo '</select>';

echo "Year: <select name='year'>";
foreach ($years as $value) {
echo '<option value="'.$value.'">'.$value.'</option>\n';
} 
?>

but how  could i echo the allready selected value in the db ???

Link to comment
Share on other sites

how would istore it as timestamp 14??   im storing it as varchar

go into phpmyadmin click the table name into structure view and edit the field and change it to timestamp then in the limitations section type 14, you can now insert it throgh your SQL as

 

 

$dob = "000000".$birthday.$birthmonth.$birthyear;

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.