将数据从excel表导入到php中的数据库时转换日期

时间:2012-12-12 12:31:15

标签: php

我有一张格式为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);
}

?>

2 个答案:

答案 0 :(得分:1)

可悲的是,我有同样的问题!我尝试了格式化方式,没有用!

我不知道它是否适用于您,但我带来的解决方案是在它前面放了一个撇号! ('YYYY / MM / DD)。 Excel本身省略了撇号,只显示数据AS TEXT,而不是Excel时间戳。通过这种方式,您可以按“/”分解数据并按照您想要的方式放置。

但是,如果你不可能在每一个中放入一个撇号,你可以验证是否是一个数字并尝试convert the excel timestamp to unix timestamp,假设你服务器是* NIX之类的。

=)

我知道这不是最好的解决方案,但到目前为止我能得到的是。希望它可以帮到你。

答案 1 :(得分:0)

试试这个

    $date_format = floor($excelDateTime);
    $time_format = $excelDateTime - $date_format;
    $mysql_strdate = ($date_format > 0) ? ( $date_format - 25569 ) * 86400 + $time_format * 86400 :    $time_format * 86400;
    $mysql_date_format = date("Y-d-m", $mysql_strdate);