I have a script which imports a csv file into a MySQL table using explode to separate the fields. my last field in the csv file is a date time field but in the incorrect format as per image below:

I want to use the below code to insert the csv into MySQL and change the format of the last column from mm/dd/yyyy hh:mm:ss am/pm to the MySQL format of yyyy-mm-dd hh:mm:ss
Existing working script (just doesn’t import date time field as format is wrong):
$fieldseparator = ",";
$lineseparator = "\n";
$csvfile = "LoadsOverWB.csv";
foreach(split($lineseparator,$csvcontent) as $line) {
$lines++;
$line = trim($line," \t");
$line = str_replace("\r","",$line);
/************************************
This line escapes the special character. remove it if entries are already escaped in the csv file
************************************/
$line = str_replace("'","\'",$line);
/*************************************/
$linearray = explode($fieldseparator,$line);
$linemysql = implode("','",$linearray);
if($addauto)
$query = "replace into $databasetable(loadnumber,weighbillnumber,VehicleRegistration,haulier,vehicleweight,rolledproductkg,hegrosskg,roofinhkg,nonmetalkg,wbtotalkg,datetime) values('','$linemysql');";
else
$query = "replace into $databasetable(loadnumber,weighbillnumber,VehicleRegistration,haulier,vehicleweight,rolledproductkg,hegrosskg,roofinhkg,nonmetalkg,wbtotalkg,datetime) values('$linemysql');";
$queries .= $query . "\n";
@mysql_query($query);
}
Thanks in advance as always, much appreciated.
Just add two lines after exploding
Also before inserting print_r the result to see if the format has been changed.
EDITS :
Looking at the library you are using i assume the last index of each row is your date column(TIME OVER WEB). So i am count the length of array and count – 1 gives me the last index so than i am applying php date function.
date(‘format’,strtotime(datevalue))
strtotime converts the string given to numbers like 32431234 and then date function takes tow parameters. Format and number. I am sure that’s enough for you to understand.