Jump to content

Entering dates in a form field


Bhaal

Recommended Posts

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!)
Link to comment
Share on other sites

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
Link to comment
Share on other sites

[!--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] . "'";
Link to comment
Share on other sites

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 set
birthdate = '" . date('Y-m-d',strtotime($_POST['birthdate'])) . "'";
?>[/code]
Ken
Link to comment
Share on other sites

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?



Link to comment
Share on other sites

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.

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.