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')";

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.