Hi all,

I'm sure this has been asked before, but I have been unable to figure out how to do it. I am asking customers for a date in the form mm/dd/yy. I am inserting all of the form's information into my MySQL database. Everything inserts just fine (Well, I'm still working on the radio buttons problem, but that's another problem.), but of course the date format for MySQL is not mm/dd/yy; it's yyyy/mm/dd.

Can anyone help me with the conversion required?

Jerry Nielsen

Rose Arbour recalls and restores a pace of life from an earlier era and
allows you sip and savour life instead of gulping it down.

                    [url]http://www.rosearbour.com[/url]

    Well, the obvious suggestion would be that you change your form! use drop downs, then create a single string out of the three values once submitted in the format required.

    Alternatively, explode() your submitted value using "/" as the marker, then reassemble the key/values into the correct order. BUT because this relies upon correct text inputting on the part of your user is far more likely to fail because you'd be amazed how man7 people just don't read the instructions and will simply input a date in the format they are used to using (for me that would 04/04/69 ie dd/mm/yy !

      <?php 
      $DateOld	= "12/31/02";
      $ConvDate	= explode("/",$DateOld);
      $DateNew	= "20".$ConvDate[2]."-".$ConvDate[0]."-".$ConvDate[1];
      print "DateOld: $DateOld -> DateNew: $DateNew";
      ?> 
      

        hand,

        Thank you! That was the little bit of help I needed. All is working just as I want it to now.

        Jerry Nielsen

          2 years later

          HTML form date mm/dd/yy to MySQL date
          how do you convert the 3 values to one string (and can anyone do it is asp)

          i have 3 drop down boxes, day month year, need one string value to write to mysql.

          i can do the format from mm/dd/yy to MySQL date. just dont know how to make the 3 values to 1

            This could be the wrong way to do it, but I've had success with the following code -

            $dateUF=strtotime($submittedDate);
            $dateF=date("Y-m-d", $dateUF);
            

            This'll take the submitted date field, convert it to a timestamp ($dateUF), then convert the timestamp to the MySQL format date ($dateF). Works to and from the database.

            jay_suz - simply concantenate the fields, like so -

            $dateF = $year."-".$month."-".$day;
            

            You'll also need to make sure that any day or month specification includes the preceding 0, but I can't remember off the top of my head how to take car of that one. sorry...

            HTH!

              Write a Reply...