1
require_once 'PHPExcel.php';
require_once 'PHPExcel/IOFactory.php';

//Values
$path = getcwd(); 

        $latest_ctime = 0;
        $latest_filename = '';    

        $d = dir($path);
        while (false !== ($entry = $d->read())) {
        $filepath = "{$path}/{$entry}";
        // could do also other checks than just checking whether the entry is a file
        if (is_file($filepath) && filectime($filepath) > $latest_ctime) {
          $latest_ctime = filectime($filepath);
         $latest_filename = $entry;
        }}

//Reading Excel File

$objPHPExcel = PHPExcel_IOFactory::load($latest_filename);

foreach ($objPHPExcel->getWorksheetIterator() as $worksheet){
     $worksheetTitle = $worksheet->getTitle();
     $highestRow = $worksheet->getHighestRow(); // e.g. 10 For all rows
     $highestColumn = $worksheet->getHighestColumn(); // e.g 'F'
     $highestColumnIndex = PHPExcel_Cell::columnIndexFromString($highestColumn);
}

//Calculationg Columns
$nrColumns = ord($highestColumn) - 64;

//Displaying Sheet Details

echo "File name: " . $path;
echo '<br>';
echo "Sheet name: " . $worksheetTitle;
echo '<br>';
echo "No. of Columns: " . $nrColumns;
echo '<br>';
echo "No. of Rows: " . $highestRow;
echo '<br><br>';
$i=1;
$j=0;
for ($row = 2; $row <= $highestRow; ++ $row) {
$val=array();
for ($col = 0; $col < $highestColumnIndex; ++ $col) {
$cell = $worksheet->getCellByColumnAndRow($col, $row);
$val[] = $cell->getValue();
}
$var=$val[2];   //the date value is in the 3rd column
echo date('d/m/Y H:i:s', PHPExcel_Shared_Date::ExcelToPHP($var)); 
echo "<br>";

我面临的问题是将从 excel 文件读取的 int 时间值转换为要插入 DB 的 mysql 日期时间格式。我的 excel 文件具有像 19-09-2015 00:00:00 这样的值,当我使用上面的代码时转换为 19-09-2015 02:00:00,这比 excel 数据中的值多 2 小时,所有日期值都发生这种情况,似乎增加了 2 小时,我如何让它打印正确的值。

4

1 回答 1

0

您的层时区显然有问题。检查 PHP(您可以从相应的 PHP 文档开始)和 MySQL 的时区,并确保它们匹配。

于 2015-09-22T13:33:39.283 回答