runnerjp Posted February 2, 2009 Share Posted February 2, 2009 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" Quote Link to comment https://forums.phpfreaks.com/topic/143450-storing-dob-into-database/ Share on other sites More sharing options...
Snart Posted February 2, 2009 Share Posted February 2, 2009 Formatted dates can be a huge pain in the back to work with, especially on db-level. I'd convert the date into a timestamp with mktime() and store it as an INT. Quote Link to comment https://forums.phpfreaks.com/topic/143450-storing-dob-into-database/#findComment-752472 Share on other sites More sharing options...
Yesideez Posted February 2, 2009 Share Posted February 2, 2009 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? Quote Link to comment https://forums.phpfreaks.com/topic/143450-storing-dob-into-database/#findComment-752473 Share on other sites More sharing options...
GingerRobot Posted February 2, 2009 Share Posted February 2, 2009 Indeed. Store it in a DATE field. And yeah, just implode it. Quote Link to comment https://forums.phpfreaks.com/topic/143450-storing-dob-into-database/#findComment-752474 Share on other sites More sharing options...
RichardRotterdam Posted February 2, 2009 Share Posted February 2, 2009 use date or timestamp. it will safe you the pain later if you need calculations Quote Link to comment https://forums.phpfreaks.com/topic/143450-storing-dob-into-database/#findComment-752475 Share on other sites More sharing options...
runnerjp Posted February 2, 2009 Author Share Posted February 2, 2009 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? Quote Link to comment https://forums.phpfreaks.com/topic/143450-storing-dob-into-database/#findComment-752522 Share on other sites More sharing options...
RichardRotterdam Posted February 2, 2009 Share Posted February 2, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/143450-storing-dob-into-database/#findComment-752524 Share on other sites More sharing options...
PFMaBiSmAd Posted February 2, 2009 Share Posted February 2, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/143450-storing-dob-into-database/#findComment-752526 Share on other sites More sharing options...
runnerjp Posted February 2, 2009 Author Share Posted February 2, 2009 ok im abit lost then on how to get from whre i am...to convert it into a date and insert it into my date field? Quote Link to comment https://forums.phpfreaks.com/topic/143450-storing-dob-into-database/#findComment-752532 Share on other sites More sharing options...
Snart Posted February 2, 2009 Share Posted February 2, 2009 Negative unix timestamps can be created with strtotime() and stored in unsigned INTs. And an INT only takes up half as much space as a datetime. Quote Link to comment https://forums.phpfreaks.com/topic/143450-storing-dob-into-database/#findComment-752533 Share on other sites More sharing options...
RichardRotterdam Posted February 2, 2009 Share Posted February 2, 2009 oh i have to take that back from parsing it to ints to have a valid date you could still do $dob='9999-99-99'; but you could use checkdate() to validate for a valid date and then turn it into date type for the database http://nl.php.net/checkdate Quote Link to comment https://forums.phpfreaks.com/topic/143450-storing-dob-into-database/#findComment-752535 Share on other sites More sharing options...
runnerjp Posted February 2, 2009 Author Share Posted February 2, 2009 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--; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/143450-storing-dob-into-database/#findComment-752542 Share on other sites More sharing options...
RichardRotterdam Posted February 2, 2009 Share Posted February 2, 2009 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 } Quote Link to comment https://forums.phpfreaks.com/topic/143450-storing-dob-into-database/#findComment-752549 Share on other sites More sharing options...
runnerjp Posted February 2, 2009 Author Share Posted February 2, 2009 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!'; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/143450-storing-dob-into-database/#findComment-752559 Share on other sites More sharing options...
nadeemshafi9 Posted February 2, 2009 Share Posted February 2, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/143450-storing-dob-into-database/#findComment-752563 Share on other sites More sharing options...
runnerjp Posted February 2, 2009 Author Share Posted February 2, 2009 sadly i use php myadmin... would i be able to use that? Quote Link to comment https://forums.phpfreaks.com/topic/143450-storing-dob-into-database/#findComment-752568 Share on other sites More sharing options...
nadeemshafi9 Posted February 2, 2009 Share Posted February 2, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/143450-storing-dob-into-database/#findComment-752575 Share on other sites More sharing options...
nadeemshafi9 Posted February 2, 2009 Share Posted February 2, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/143450-storing-dob-into-database/#findComment-752576 Share on other sites More sharing options...
runnerjp Posted February 2, 2009 Author Share Posted February 2, 2009 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 ??? Quote Link to comment https://forums.phpfreaks.com/topic/143450-storing-dob-into-database/#findComment-752582 Share on other sites More sharing options...
nadeemshafi9 Posted February 2, 2009 Share Posted February 2, 2009 store it as timstamp 14 its easy to break up, maybe there are things you can do with Date data type that the seniors know calculations etc. You havent stated what data type your using Quote Link to comment https://forums.phpfreaks.com/topic/143450-storing-dob-into-database/#findComment-752585 Share on other sites More sharing options...
runnerjp Posted February 2, 2009 Author Share Posted February 2, 2009 how would istore it as timestamp 14?? im storing it as varchar Quote Link to comment https://forums.phpfreaks.com/topic/143450-storing-dob-into-database/#findComment-752599 Share on other sites More sharing options...
nadeemshafi9 Posted February 2, 2009 Share Posted February 2, 2009 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; Quote Link to comment https://forums.phpfreaks.com/topic/143450-storing-dob-into-database/#findComment-752631 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.