0

I'm taking a date in a CSV and trying to add it to my database in mysql date format. Here's my code (or some of it):

 while (($data = fgetcsv($handle, 1000, ",")) !== FALSE)
 {
    $originalDate = $data[1];
    echo "original date format:".$originalDate."<br/>";

    //$delivery_date = date('Y-m-d', strtotime($originalDate));

    $parts = explode('/', $originalDate);
    $delivery_date = $parts[2] . '-' . $parts[1] . '-' . $parts[0]; // 09/12/2011

    echo "new date format:".$delivery_date."<br/>";

    $row++;
    $import="INSERT into dispatch (delivery_note_number, delivery_date) 
    values ('$data[0]','$delivery_date')";
    mysql_query($import) or die(mysql_error());

 }

The 'echos' are just to help me debug. The new date format $delivery_date echoes in a way that should be suitable to stick into the database e.g. 2012-04-15

However, when I check the database, all I end up with is 0000-00-00

Any ideas why mysql won't take the date?

2
  • note I've tried strtotime to convert the format, and explode, just to see if it made any difference. It hasn't. Commented Apr 22, 2012 at 13:34
  • Check your $import (echo it to see what is wrong). Your $delivery_date variable may be seen as "$delivery_date" in the query. Commented Apr 22, 2012 at 13:36

1 Answer 1

1

Change it to :

$import="INSERT into dispatch (delivery_note_number, delivery_date) 
    values ('".$data[0]."','".$delivery_date."')";

That should help.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.