frank5050 Posted April 10, 2011 Share Posted April 10, 2011 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>"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/233305-change-date-format-upon-mysql-import/ Share on other sites More sharing options...
drisate Posted April 10, 2011 Share Posted April 10, 2011 something like this? <?php $date = explode ('/', $data[2]); $mm = $date[0]; $dd = $date[1]; $yy = $date[2]; if ($yy<=11){ $yyyy = "20$yy"; }else{ $yyyy = "19$yy"; } $new_date = "$yyyy-$mm-$dd"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/233305-change-date-format-upon-mysql-import/#findComment-1199818 Share on other sites More sharing options...
Pikachu2000 Posted April 10, 2011 Share Posted April 10, 2011 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')"; Quote Link to comment https://forums.phpfreaks.com/topic/233305-change-date-format-upon-mysql-import/#findComment-1199821 Share on other sites More sharing options...
dcro2 Posted April 10, 2011 Share Posted April 10, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/233305-change-date-format-upon-mysql-import/#findComment-1199822 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.