Jump to content

Change Date Format Upon MySQL import


frank5050

Recommended Posts

Hi,

 

I am importing data from a csv file into mysql.

 

Everything works fine except that the 'order_date' column in CSV is in the following format mm/dd/yy.

 

Upon import I would like for that format to be changed to yyyy-mm-dd

 

I am not sure how to modify the following code to be able to do so.

 

Any help would be appreciated.

 

 

 

<?
include "connect.php";

if(isset($_POST['submit']))

   {

     $filename=$_POST['filename'];

     $handle = fopen("$filename", "r");

     while (($data = fgetcsv($handle, 1000, ",")) !== FALSE)

     {


$date = date("Y-m-d");


$import="INSERT into table
(order_id,
order_status,
order_date,
todays_date)
values
('$data[0]',
'$data[1]',
'$data[2]',
'$date')";
       mysql_query($import) or die(mysql_error());

     }

     fclose($handle);

     print "Import done";



   }

   else

   {



      print "<form action='importnew.php' method='post'>";

      print "Type file name to import:<br>";

      print "<input type='text' name='filename' size='20'><br>";

      print "<input type='submit' name='submit' value='submit'></form>";

   }
?> 

Link to comment
Share on other sites

Untested, but should work.

 

$formatted_date = date( 'Y-m-d', strtotime($data[2]) );
$import="INSERT into table
(order_id,
order_status,
order_date,
todays_date)
values
('$data[0]',
'$data[1]',
'$formatted_date',
'$date')";

Link to comment
Share on other sites

Untested, but should work.

 

$formatted_date = date( 'Y-m-d', strtotime($data[2]) );
$import="INSERT into table
(order_id,
order_status,
order_date,
todays_date)
values
('$data[0]',
'$data[1]',
'$formatted_date',
'$date')";

I was just about to post that, so just a note, it'll work as long as your dates are on or after 1/1/1970.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.