我有一个脚本,它使用explode 将csv 文件导入MySQL 表以分隔字段。我在 csv 文件中的最后一个字段是日期时间字段,但格式不正确,如下图所示:
我想使用下面的代码将 csv 插入 MySQL 并将最后一列的格式从 mm/dd/yyyy hh:mm:ss am/pm 更改为 yyyy-mm-dd hh:mm:ss 的 MySQL 格式
现有的工作脚本(只是不导入日期时间字段,因为格式错误):
$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);
}
一如既往地提前感谢,非常感谢。