首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将数据从excel表格导入到php中的数据库时转换日期

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

Stack Overflow用户
提问于 2012-12-12 20:31:15
回答 2查看 2.2K关注 0票数 1

我有一个格式为10/12/2012的excel工作表。然后我将其转换为csv文件,我有一个日期为"41253",当我导入到我的数据库中时,它存储为0000-00-00。如何在导入时转换日期格式。还有没有别的办法?

这是im用来导入数据库的代码。

代码语言:javascript
复制
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);
}

?>
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-10-01 00:10:05

不幸的是,我也有同样的问题!我尝试了格式化的方式,不起作用!

我不知道它是否适用于您,但是我提供的一个解决方案是在它前面放一个撇号!('YYYY/mm/dd)。Excel本身省略了撇号,只将数据显示为文本,而不是Excel时间戳。这样,您就可以通过"/“分解数据,并以您想要的方式放置它。

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

=)

我知道这不是最好的解决方案,但到目前为止我能得到的就是这些。希望能对你有所帮助。

票数 1
EN

Stack Overflow用户

发布于 2013-10-25 14:47:27

尝尝这个

代码语言:javascript
复制
    $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);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13839841

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档