Jump to content


Photo

Entering dates in a form field


  • Please log in to reply
6 replies to this topic

#1 Bhaal

Bhaal
  • Members
  • PipPipPip
  • Advanced Member
  • 60 posts

Posted 09 April 2006 - 02:33 PM

Hi...yes I've read the FAQ and have searched hi and low, but I can't find an answer I *understand*. It's SO not fun being a newbie.

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:

<input type=text name=birthdate size="23" value="<?=$a1[birthdate]?>">

The query is written like this:

$q1 = "insert into members set
         birthdate = '$_POST[birthdate]'";
         $r1 = mysql_query($q1) or die(mysql_error());

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!)

#2 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 09 April 2006 - 02:40 PM

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:
<?php
$q1 = "insert into members set
         birthdate = '" . date('Y-m-d',strtotime($_POST[birthdate]) . "'";
$r1 = mysql_query($q1) or die(mysql_error());
?>

The strtotime() function converts the input date string into a UNIX time stamp.
The date() function formats a UNIX time stamp.

Ken

#3 ari_aaron

ari_aaron
  • Members
  • PipPipPip
  • Advanced Member
  • 50 posts
  • LocationToronto, Canada

Posted 09 April 2006 - 03:43 PM

[!--quoteo(post=363001:date=Apr 9 2006, 10:40 AM:name=kenrbnsn)--][div class=\'quotetop\']QUOTE(kenrbnsn @ Apr 9 2006, 10:40 AM) View Post[/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:
<?php
$q1 = "insert into members set
         birthdate = '" . date('Y-m-d',strtotime($_POST[birthdate]) . "'";
$r1 = mysql_query($q1) or die(mysql_error());
?>

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])) . "'";

#4 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 09 April 2006 - 03:48 PM

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 ...
<?php
$q1 = "insert into members set
birthdate = '" . date('Y-m-d',strtotime($_POST['birthdate'])) . "'";
?>
Ken

#5 Bhaal

Bhaal
  • Members
  • PipPipPip
  • Advanced Member
  • 60 posts

Posted 09 April 2006 - 04:12 PM

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:

birthdate = '" . date('Y-m-d',strtotime($_POST['birthdate'])) . "'";

So I tried:

birthdate = '" . date('Y-m-d',strtotime($_POST['birthdate'])) . "'",

And a few other iterations - always get the same error:

Parse error: syntax error, unexpected T_STRING

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?





#6 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 09 April 2006 - 04:31 PM

Please post the entire chunk of code that is giving you problems...

Ken

#7 Bhaal

Bhaal
  • Members
  • PipPipPip
  • Advanced Member
  • 60 posts

Posted 10 April 2006 - 12:06 AM

OK, here's the chunk of 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();
    }
    
}

Obviously, the line 'birthdate =' should end in a comma not a semicolon (right?), but either one produces a parce error.






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users