0

I am writing a page that will accept some info from user and get submitted as form. I wanted the date included in the form data so I wrote this little snippet to insert the current date when form is submitted:

      <li>
         <input type="hidden" id="date" name="Date_val" value="date">
           <script type="text/javascript">
              document.getElementById('date').value = Date();
           </script>             
      </li>  

Now when I print out my form all I get is a nonsense date 11--00-01 printed in my table. Looking at the _POST info this is what is getting sent to MySQL:

Array ( [formID] => 42796558181164 [Date_val] => Wed Oct 22 2014 17:50:43 GMT-0400 (Eastern Standard Time) etc...

I'm assuming that is why I get gibberish as my output. I have the MySql object defined as a simple date format. Can anyone tell me how I should format the date so MySQL will accept it and will output correctly?

Thanks...

Answer:

<li> 
         <input type="hidden" id="date" name="Date_val" value="date"> 
           <script src="jscripts/jquery.min.js" type="text/javascript"></script>
           <script type="text/javascript">
              date_tda = new Date().toISOString().slice(0, 19).replace('T', ' ');
              document.getElementById('date').value = date_tda;
           </script>             
      </li>  
Alan
  • 1,067
  • 1
  • 23
  • 37
  • 1
    I think this question has already been answered here http://stackoverflow.com/questions/5129624/convert-js-date-time-to-mysql-datetime – nsarno Oct 22 '14 at 22:16
  • You should use a universal format (e.g. a UNIX timestamp or ISO 8601), and send that to your server. On the server, transform the date into the correct timezone and a format that is understood by MySQL. And, of course, do *not* just insert external data into MySQL without proper filtering! – lxg Oct 22 '14 at 22:21
  • Thanks Kilroy that post had the answer. I wanted to give you credit but no way to. – Alan Oct 22 '14 at 23:19
  • Just a note on formatting - if you found an answer or you would like to answer your own question, you can post it as an answer and accept your own answer. – Wold Oct 22 '14 at 23:21

1 Answers1

0

Answer:

<li> 
         <input type="hidden" id="date" name="Date_val" value="date"> 
           <script src="jscripts/jquery.min.js" type="text/javascript"></script>
           <script type="text/javascript">
              date_tda = new Date().toISOString().slice(0, 19).replace('T', ' ');
              document.getElementById('date').value = date_tda;
           </script>             
      </li>  
Alan
  • 1,067
  • 1
  • 23
  • 37