0
  1. I have a form with a date field.
  2. I select a date using a jquery datepicker. The format is mm/dd/yyyy.
  3. The date inserts into the db like 0000-00-00.
  4. When I use die($startdate); This outputs the correct date in the correct format when uncommented.

db type: date

Code:

$startdate = date('Y-m-d', strtotime($_POST['startdate']));
  //die($startdate); //<--outputs the correct date in the correct format when uncommented.

  try {

 $stmt = $db->prepare('INSERT INTO table (firstname,lastname,startdate) VALUES (:firstname, :lastname, :startdate)');
  $stmt->execute(array(
    ':firstname' => $_POST['firstname'],
    ':lastname' => $_POST['lastname'],
    ':startdate' => $_POST['startdate'],

   ));

  }

I also tried

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

in the try{} but doesn't work either.

What am I doing wrong?

This is to John Conde. I believe it's quite simple on how this is different and why it's not a duplicate. If you didn't notice there was an "ERROR" in my code. I needed help finding the issue. Marc found it and now it works. I still wonder why this is so difficult to understand.

Jad
  • 163
  • 1
  • 12
  • 1
    This question has been asked multiple times on stackoverflow. Please search it, you will definitely find your answer. – Ruchi Sep 04 '15 at 21:47

1 Answers1

1

Spot the difference:

$startdate = date('Y-m-d', strtotime($_POST['startdate']));
^^^^^^^^^^

    ':startdate' => $_POST['startdate'],
                    ^^^^^^^^^^^^^^^^^^^^

You convert the date properly, but then use the original un-converted version in the query.

Marc B
  • 356,200
  • 43
  • 426
  • 500
  • Thanks Marc. I see they are the same and I'm very new to this. I'm assuming $startdate = date('Y-m-d', strtotime($_POST['startdate'])); should be something like $startdatediff = date('Y-m-d', strtotime($_POST['startdate'])); ? I did just that but db still showing 0000-00-00 – Jad Sep 04 '15 at 22:07
  • For those of you at my level Marc means this ':startdate' => $startdate . It's working now. – Jad Sep 04 '15 at 22:47