Bhaal Posted April 9, 2006 Share Posted April 9, 2006 Hi...yes I've read the FAQ and have searched hi and low, but I can't find an answer I *understand*. [b]It's SO not fun being a newbie.[/b]Anyway...I have a form that has a field for entering a birth date.This is inserted into a MySQL date field defined as 'DATE'. The format MySQL Date fields require is YY-MM-DD. Not really user friendly. (Isn't this a very common issue?)Is there a way to allow users to enter "any" date format into the form, then convert it for inserting into the database? I'm sure that is...I just don't get how it would work. The form field is named 'birthdate'; the mySQL field name is 'birthdate' (funny how that worked out).So...the form field is written like this:[code]<input type=text name=birthdate size="23" value="<?=$a1[birthdate]?>">[/code]The query is written like this:[code]$q1 = "insert into members set birthdate = '$_POST[birthdate]'"; $r1 = mysql_query($q1) or die(mysql_error());[/code]I'm pretty sure I need to use strtotime....but how? Where? Thanks for any insight! (BTW, my birthday is this Monday, April 10...it would be a good birthday indeed if I could get this working!) Quote Link to comment https://forums.phpfreaks.com/topic/6943-entering-dates-in-a-form-field/ Share on other sites More sharing options...
kenrbnsn Posted April 9, 2006 Share Posted April 9, 2006 Actually the date format in MySQL wants the format to be YYYY-MM-DD.You can use the strtotime() function reliably only if the date is after January 1, 1970. Dates before that may work on some other machines but there is no guarentee.Try this:[code]<?php$q1 = "insert into members set birthdate = '" . date('Y-m-d',strtotime($_POST[birthdate]) . "'";$r1 = mysql_query($q1) or die(mysql_error());?>[/code]The strtotime() function converts the input date string into a UNIX time stamp.The date() function formats a UNIX time stamp.Ken Quote Link to comment https://forums.phpfreaks.com/topic/6943-entering-dates-in-a-form-field/#findComment-25218 Share on other sites More sharing options...
ari_aaron Posted April 9, 2006 Share Posted April 9, 2006 [!--quoteo(post=363001:date=Apr 9 2006, 10:40 AM:name=kenrbnsn)--][div class=\'quotetop\']QUOTE(kenrbnsn @ Apr 9 2006, 10:40 AM) [snapback]363001[/snapback][/div][div class=\'quotemain\'][!--quotec--]Actually the date format in MySQL wants the format to be YYYY-MM-DD.You can use the strtotime() function reliably only if the date is after January 1, 1970. Dates before that may work on some other machines but there is no guarentee.Try this:[code]<?php$q1 = "insert into members set birthdate = '" . date('Y-m-d',strtotime($_POST[birthdate]) . "'";$r1 = mysql_query($q1) or die(mysql_error());?>[/code]The strtotime() function converts the input date string into a UNIX time stamp.The date() function formats a UNIX time stamp.Ken[/quote]just to note, isn't it$q1 = "insert into members set birthdate = '" . date('Y-m-d',strtotime($_POST[birthdate])[b])[/b] . "'"; Quote Link to comment https://forums.phpfreaks.com/topic/6943-entering-dates-in-a-form-field/#findComment-25236 Share on other sites More sharing options...
kenrbnsn Posted April 9, 2006 Share Posted April 9, 2006 Yes, that's a bad habit of mine ... leaving off the last closing parenthesis and catching later...Also I forgot the single quotes around the index ...[code]<?php$q1 = "insert into members setbirthdate = '" . date('Y-m-d',strtotime($_POST['birthdate'])) . "'";?>[/code]Ken Quote Link to comment https://forums.phpfreaks.com/topic/6943-entering-dates-in-a-form-field/#findComment-25238 Share on other sites More sharing options...
Bhaal Posted April 9, 2006 Author Share Posted April 9, 2006 Hmmm...getting a syntax error - with every interation in this thread.Plus - in reality, this isn't the only field being inserted with the query - and it's not the last one in the list (although it could be)...so I think it needs to end with a comma in stand of a semicolon.This was the final one from kenrbnsn:[code]birthdate = '" . date('Y-m-d',strtotime($_POST['birthdate'])) . "'";[/code]So I tried:[code]birthdate = '" . date('Y-m-d',strtotime($_POST['birthdate'])) . "'",[/code]And a few other iterations - always get the same error:[code]Parse error: syntax error, unexpected T_STRING[/code]Doncha just hate that?(Thanks very much for this...I feel like it's close...)BTW, will this work on an Update query as well as an Insert query? Quote Link to comment https://forums.phpfreaks.com/topic/6943-entering-dates-in-a-form-field/#findComment-25243 Share on other sites More sharing options...
kenrbnsn Posted April 9, 2006 Share Posted April 9, 2006 Please post the entire chunk of code that is giving you problems...Ken Quote Link to comment https://forums.phpfreaks.com/topic/6943-entering-dates-in-a-form-field/#findComment-25246 Share on other sites More sharing options...
Bhaal Posted April 10, 2006 Author Share Posted April 10, 2006 OK, here's the chunk of code:[code] $q1 = "update members set username = '$_POST[username]', FirstName = '$_POST[FirstName]', LastName = '$_POST[LastName]', age = '$_POST[age]', sex = '$_POST[sex]', hometown = '$_POST[hometown]', homestate = '$_POST[hstate]', relocate = '$_POST[relocate]', internationalOK = '$_POST[internationalOK]', reason = '$_POST[reason]', out = '$_POST[out]', legalstatus = '$_POST[legalstatus]', height = '$_POST[height]', weight = '$_POST[weight]', birthdate = '" . date('Y-m-d',strtotime($_POST['birthdate'])) . "'"; hair = '$_POST[hair]', eyes = '$_POST[eyes]', email = '$_POST[yemail]', telephone = '$_POST[telephone]', Country = '$_POST[country]', StandardAds = '$NewStandard', FeaturedAds = '$_POST[featured]', ExpDate = '$NewDays' where MemberID = '$_GET[MemberID]' "; mysql_query($q1); if(!mysql_error()) { echo "The information was updated successfully!"; } else { echo mysql_error(); } }[/code]Obviously, the line 'birthdate =' should end in a comma not a semicolon (right?), but either one produces a parce error. Quote Link to comment https://forums.phpfreaks.com/topic/6943-entering-dates-in-a-form-field/#findComment-25364 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.