【问题标题】:I'm Import to Mysql from Excel files, but the results are 1970-01-01我是从 Excel 文件导入到 Mysql,但结果是 1970-01-01
【发布时间】:2016-07-05 20:12:10
【问题描述】:

我是从 Excel 文件导入到 Mysql,但结果是 1970-01-01,这是我的代码:

// server info
$server = 'localhost';
$user = 'root';
$pass = '';
$db = 'agenda';

// connect to the database
$mysqli = new mysqli($server, $user, $pass, $db);

// show errors (remove this line if on a live site)
mysqli_report(MYSQLI_REPORT_ERROR);

?>

<?php
require 'classes/phpexcel.php';
require_once 'classes/phpexcel/iofactory.php';
$inputFileName  = 'database/sql/agenda.xlsx';
$inputFileType  = 'Excel2007';
$objReader      = PHPExcel_IOFactory::createReader("$inputFileType");
$objReader->setReadDataOnly(true);
$objPHPExcel    = $objReader->load("$inputFileName");
$objWorksheet   = $objPHPExcel->getActiveSheet();
$highestRow     = $objWorksheet->getHighestRow();
$highestColumn  = $objWorksheet->getHighestColumn();
$highestColumnIndex = PHPExcel_Cell::columnIndexFromString($highestColumn);

// view in table
echo '<table>' . "\n";
for ($row = 1; $row <= $highestRow; ++$row) {
    echo '<tr>' . "\n";
    for ($col = 0; $col <= $highestColumnIndex; ++$col) {
        echo '<td>' . $objWorksheet->getCellByColumnAndRow($col, $row)->getValue() . '</td>' . "\n";
    }
    echo '</tr>' . "\n";
}
echo '</table>' . "\n";

//input to mysql
for($row = 2; $row <= $highestRow; ++$row) {
    for($col = 0; $col < $highestColumnIndex; ++$col) {  
        $rows[$col] = $objWorksheet->getCellByColumnAndRow($col, $row); 
    }
    $id   ="$rows[0]";
    $ts   ="$rows[1]";
    $pd   ="$rows[2]";

    $get_data1  = "$rows[3]";
    $st   = date("Y-m-d", strtotime ($get_data1));

    $stm  ="$rows[4]";

    $get_data2  ="$rows[5]";
    $et   = date("Y-m-d", strtotime ($get_data2));

    $etm  ="$rows[6]";
    $g    ="$rows[7]";
    $w    ="$rows[8]";
    // Prepared Statement
    $stmt = $mysqli->prepare("INSERT INTO `keyin` VALUES ('$id', '$ts', '$pd', '$st', '$stm', '$et', '$etm', '$g', '$w')");
    //Prepared Statement Bound$stmt->bind_param('sssssssss', $id, $ts, $pd, $st, $stm, $et, $etm, $g, $w);     


    //Prepared Statement Executed 
    $stmt->execute();
    printf("%s Row Inserted.\n", $stmt->affected_rows);
    //Prepared Statement Closed
    $stmt->close();

    // If you don't want to use prepared statement, you can use this one

    $mysqli->query("INSERT INTO users (Username,Email,Gender,Country) VALUES ('$rows[0]', '$rows[1]', '$rows[2]', '$rows[3]')");
}
$mysqli->close();

【问题讨论】:

  • 听起来像是 $get_data1/$rows[3] 中的无效日期/时间戳。 var_dump($rows[3]) 能得到什么?
  • 我从 excel 文件中得到的结果是 = 42604
  • 这不是时间戳或字符串格式的日期。确定它是正确的列? excel 中显示的等效日期是多少?
  • @Jeff 时间戳在 excel 中不计入 PHP 中。在 Excel 中,“时间”从 1900 开始,数字是天数,而不是 PHP 中的秒数。

标签: php


【解决方案1】:

由于 excel 计算自 1900-01-01 以来的天数,因此您不能使用 strtotime()。

您需要做的是将 excel 天数转换为秒数。

$unix = ($get_data1 - 25569) * 86400; 
$st = date("Y-m-d", $unix);

您的 ~42000 的 excel 值小于以秒为单位的一天 (86400),因此您在 php 中得到 1970-01-01。
如果您在 php 代码中添加时间,您会看到时间大约是 1970 年 1 月 1 日中午。

https://3v4l.org/teI1F

【讨论】:

  • $st = PHPExcel_Style_NumberFormat::toFormattedString($get_data1, 'YYYY-MM-DD');
  • 很抱歉忘记从代码中删除 strtotime(),即使我写了它也不能使用....facepalm
  • @EdiHaryono 我不知道它被取消了。但话又说回来,我经常使用 excel 并且我在 php 中编程了很多,但我从未将两者结合起来。好一个!我只知道如何用蛮力解决问题;-)
【解决方案2】:

试试这个:

$st = date('Y-m-d', PHPExcel_Shared_Date::ExcelToPHP($get_data1));

【讨论】:

  • 欢迎。如果您觉得有用,请为答案投票。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-03-03
  • 1970-01-01
  • 2015-04-12
  • 1970-01-01
  • 2014-01-16
  • 2015-05-21
  • 1970-01-01
相关资源
最近更新 更多