【发布时间】:2011-03-31 19:29:05
【问题描述】:
我有一个包含 5 列的电子表格。其中之一包含格式为 02-Dec-10 的日期列。
当我将该 Excel 表格输入数据库时,它会将空日期存储为 0000-00-00 00:00:00。
如何将其存储为日期?
编辑
这是使用 CSV 文件向数据库提供数据的代码
$date = date("d-m-Y");
$databasetable = "sample";
$fieldseparator = ",";
$lineseparator = "\n";
$csvfile = "../uploads/" . basename( $_FILES['file']['name']);
$addauto = 0;
$save = 1;
$outputfile = "../temp/output.sql";
if(!file_exists($csvfile)) {
#echo $csvfile;
echo "File not found. Make sure you specified the correct path.\n";
return 0;
exit;
}
$file = fopen($csvfile,"r");
if(!$file) {
#echo "Error opening data file.\n";
return 0;
exit;
}
$size = filesize($csvfile);
if(!$size) {
echo "File is empty.\n";
return 0;
exit;
}
$csvcontent = fread($file,$size);
fclose($file);
$lines = 0;
$queries = "";
$linearray = array();
foreach(split($lineseparator,$csvcontent) as $line) {
$lines++;
$line = trim($line," \t");
$line = str_replace("\r","",$line);
$line = str_replace("'","\'",$line);
$linearray = explode($fieldseparator,$line);
$linemysql = implode("','",$linearray);
if($addauto)
$query = "insert into $databasetable values('','$linemysql');";
else
$query = "insert into $databasetable values('$linemysql');";
$queries .= $query . "\n";
@mysql_query($query);
}
@mysql_close($con);
if($save) {
$file2 = fopen($outputfile,"w");
if(!$file2) {
echo "Error writing to the output file.\n";
return 0;
}
else {
fwrite($file2,$queries);
fclose($file2);
return 1;
}
}
//echo "Found a total of $lines records in this csv file.\n";
【问题讨论】:
-
你用什么样的方法使用什么样的库使用代码输入什么样的数据库?
-
请停止发布重复的内容,尤其是如果新的和另一个的措辞一样不清楚,并且那里已经提供了大量的帮助。
-
这不是重复的伙伴......我错误地发布了那个问题。这个问题无关紧要
-
@Rajasekar 很好,但是请停止发布重复内容。这是在浪费每个人的时间,也是对在另一个问题上帮助过你的人不尊重的表现。跨度>
-
没什么大不了的,只是不是一件好事。无论如何,我会通知版主合并这两个问题,因为它们都有答案。这样就解决了。