0

I am using a form to submit date, subject, content text and an image to mysql using php. I see that all the data get inserted into the DB properly except date which shows up as 0000-00-00 in the table row.

I have unable to figure out how to change date format to be sent to DB.

<form action="#" enctype="multipart/form-data" method="post">

        <div class="md-form col-md-6">
          <label for="date-picker-example"> Event Date : </label>
          <input type="date" name='news_event_date' id="date-picker-example" class="form-control datepicker"/>
        </div>

        <div class="md-form col-md-6">
            <label for="form7">Update Header:</label>
            <textarea id="form7" class="md-textarea form-control" rows="1" type="text" name="news_header" ></textarea>
        </div>

          <div class="md-form col-md-6">
          <label for="form8">Update Body:</label>
          <textarea type="text" id="form8" class="md-textarea form-control" name="news_body" rows="2" ></textarea>
        </div>

        <div class="md-form col-md-6">
          <label for="form9">Password : </label>
          <input id="form9" type="password" name="news_password" class="md-textarea form-control" autocomplete="new-password"/>
        </div>

              <div class="md-form col-md-6">
          <div class="file-field">
              <div class="btn btn-primary btn-sm float-left">
                  <span>Choose files</span>
                  <input type="file" accept="image/*" name="news_single_file">
              </div>
              <div class="file-path-wrapper">
              <input  class="file-path validate" type="text" placeholder="Upload one or more files">
              </div>
          </div>
          </div>

          <button class="btn btn-outline-info btn-rounded btn-block z-depth-0 my-4 waves-effect" type="submit" name="news_submit" value="Subm"/>
  <!--//this div is for latest news-->
  </form>

All values are updated as required apart from date.

Til
  • 5,150
  • 13
  • 26
  • 34
  • Give us some examples of how the date is being sent. If it is the wrong format then the database might reject it (needs to be in YYYY-MM-DD format). –  May 27 '19 at 13:01
  • that's the big question I am trying to find an answer for, I would like to know how to change the date format being sent to DB. – peacewalker May 27 '19 at 13:03
  • That information should be in the javascript library. You could do it in php i.e. $dt = new DateTime( $_POST['date'] ); $newDate = $dt->format("Y-m-d'); –  May 27 '19 at 13:07

2 Answers2

0

Which format of date you use in the datepicker?

If you want to store date from datepicker to DB you should set right format when you initialize Datepicker:

$( ".selector" ).datepicker({ dateFormat: "yy-mm-dd" });

Default datepicker format is "mm/dd/yy"

Another option, you can process date before storing it in the DB (convert it from one format to another). Keep in mind that if you want to show date in case of form errors, you should convert it back and show in form in the initial format.

Michael Krutikov
  • 484
  • 1
  • 4
  • 10
  • I am using this template for datepicker https://mdbootstrap.com/docs/jquery/forms/date-picker/ I tried yy-mm-dd but it does not seem to work. – peacewalker May 27 '19 at 13:39
0

Try formatSubmit parameter:

$('.datepicker').pickadate({

formatSubmit: 'yyyy-mm-dd',

})

Michael Krutikov
  • 484
  • 1
  • 4
  • 10