我有一个格式为 10/12/2012 的 excel 表。然后我将其转换为 csv 文件,我的日期为“41253”,当我将其导入我的数据库时,它存储为 0000-00-00。如何在导入时转换日期格式。还是有其他方法?
这是我用来导入数据库的代码
if(isset($_POST['Submit']))
{
GLOBAL $HTTP_POST_FILES;
//$path= $HTTP_POST_FILES['ufile']['name'];
$path= $_FILES['ufile']['name'];
//
if(copy($_FILES['ufile']['tmp_name'], $path))
{
$host="localhost"; // Host name
$username="root"; // Mysql username
$password=""; // Mysql password
$db_name="xyz"; // Database name
$tbl_name="productsegmentmaster"; // Table name
require_once '.\phpExcelReader\Excel\reader.php';
$excel = new Spreadsheet_Excel_Reader();
$excel->setOutputEncoding('CP1251');
$excel->read($path);
$x=2;
$sep = "*";
ob_start();
while($x<=$excel->sheets[0]['numRows']) {
$y=1;
$row="";
while($y<=$excel->sheets[0]['numCols']) {
$cell = isset($excel->sheets[0]['cells'][$x][$y]) ? $excel->sheets[0]['cells'][$x][$y] : '';
$row.=($row=="")?"".$cell."":"".$sep."".$cell."";
$y++;
}
echo $row."\n";
$x++;
}
//echo "Sheet count:$x";
$fp = fopen("data.csv",'w');
fwrite($fp,ob_get_contents());
fclose($fp);
//echo"----";
ob_end_clean();
//connect to the database
$connect = mysql_connect("localhost","root","");
mysql_select_db("AmaraRaja",$connect); //select the table
//$file = $_FILES['data.csv']['tmp_name'];
$handle = fopen('data.csv',"r");
//loop through the csv file and insert into database
global $data;
do {
//echo "$data[0]";
//echo "$data[1]";
//echo "$data[2]";
//$post['ProductSegmentCode'] = $data[0];
// $post['ProductSegment'] = $data[1];
// $post['ProductGroup'] = $data[2];
$post['ProductCode'] = $data[0];
$post['WarrantyPeriod'] = $data[1];
$post['ProRataPeriod'] = $data[2];
$test = date("Y-m-d",strtotime($data[3]));
$post['ManufactureDate'] = $test;
$test1 = date("Y-m-d",strtotime($data[4]));
echo $data[4];
$post['ApplicableFormDate'] = $test1;
$tname = "productwarranty";
try
{
if($data[0]!="")
{
$news->addNews($post,$tname);
//mysql_query("INSERT INTO productsegmentmaster VALUES ( '$data[0]', '$data[1]','$data[2]')");
}
}
catch(Exception $e)
{
echo $e.getMessage();
}
} while ($data = fgetcsv($handle,1000,"*","'"));
echo"<script>alert('File $path Imported Successfully')</script>";
}
fclose($handle);
unlink('data.csv');
unlink($path);
}
?>