【问题标题】:Why PHP Excel reader output one more day from CSV?为什么 PHP Excel 阅读器从 CSV 输出多一天?
【发布时间】:2018-07-07 21:19:51
【问题描述】:

我正在使用 excel/reader.php 读取 CSV 文件并获取数据。

CSV 中的日期字段的值为:20/10/2014

Customer Name    Date
Lorem Spem       20/10/14

当我在使用 PHP 读取 CSV 后打印此内容时:

        $file_name = $_FILES['file']['tmp_name'];
        $library_path = getcwd().'/application/libraries/excel/reader.php';
        require_once $library_path;
        $excel = new Spreadsheet_Excel_Reader();
        $excel->setOutputEncoding('CP1251');
        $status = $excel->read($file_name);
        $totalSheets = count($excel->sheets);
        for($sheetCount = 0 ; $sheetCount < $totalSheets ; $sheetCount++)
        {
            $excel_data = $excel->sheets[$sheetCount]['cells'];
            $totalRows = $excel->sheets[$sheetCount]['numRows'];
            print_r($excel_data);
        }

它给出了结果:21/10/2014

Array ( [1] => Lorem Spem [2] => 21/10/2014 )

我不知道为什么它会在 CSV 的日期上加一天。

【问题讨论】:

  • 用户需要查看您的php 代码添加您的代码如this
  • 可能是时区问题;但没有更多信息,我们只能猜测
  • @Naruto:我已经用有用的信息更新了问题,请检查是否可以找到,为什么 excel 阅读器会在输出中添加 1 天。
  • 请张贴(相关部分)您的代码。但我确实喜欢@MarkBaker 对时区的怀疑。尝试在一天中的不同时间运行它,看看会发生什么。
  • 如果只是CSV文件,何不干脆使用PHP内置的fgetcsv()函数;那么您就可以完全控制数据的每个元素

标签: php phpexcelreader


【解决方案1】:

我发现代码是正确的:它只在本地主机上的 Excel 表中给日期添加了一天。

当我将此代码上传到 Live Server 时,它给出了正确的日期。

我认为这又是时区问题。由于我尝试读取的 CSV 是在加拿大时区制作的,而我尝试在印度时区读取,所以显示日期错误。

但是当我将代码上传到加拿大的实时服务器时,它开始从 Excel 表中给出正确的日期。

【讨论】:

    猜你喜欢
    • 2012-01-19
    • 2019-10-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多