【问题标题】:Read excel date value to m-d-Y in php在php中将excel日期值读取到m-d-Y
【发布时间】:2013-02-01 09:12:40
【问题描述】:

在我的 php 项目中,我需要导入一个 excel 文件并将所有值插入到表中。 我正在使用phpExcelReader 导入文件.. 在 y excel 文件中,有一列用于以 d/m/Y 格式存储日期值。但是当我读到这些值时,它是不正确的 敌人的例子,我将日期指定为“25/02/2013”,结果将是TueTue/FebFeb/2013201320132013

查看代码

require_once 'Excel/reader.php';
$data = new Spreadsheet_Excel_Reader();
$data->setOutputEncoding('CP1251');
//echo $target_path;
$data->read('uploads/initiativeData/'.$uploadFile);     
$startDate=trim(mysql_escape_string($data->sheets[0]['cells'][$i][5]));

这里$startDate是存储日期字段值的变量

我需要得到02-25-2013..的值。有人知道吗

请回复

谢谢

【问题讨论】:

标签: php


【解决方案1】:

这解决了我的问题:

$data->sheets[$sheetNumber]['cellsInfo'][$rowNumber][$cellNumber]['raw'];

它会给你时间戳格式的日期 其中:

$data = new Spreadsheet_Excel_Reader();
$data->read("YourFile.xls");

【讨论】:

    【解决方案2】:

    试试strtotime,它应该可以解析你的日期字符串。之后,您可以使用date 将其转换为您想要的任何格式。例如:

    $yourData = date('d.m.Y', strtotime(trim($data->sheets[0]['cells'][$i][5])));
    

    编辑:

    $yourData = date('d.m.Y', strtotime(str_replace('/', '-', trim($data->sheets[0]['cells'][$i][5]))));
    

    【讨论】:

    • 我试过这个,但我得到的日期是 01.01.1970,实际上结果是 02-25-2013
    • 刚刚更新了我的答案,看来strtotime 并没有我想的那么酷。 :-)
    • 感谢您的帮助。但它仍然不起作用。我得到的结果是'01.01.1970'。请帮帮我
    • echo date('d.m.Y', strtotime(str_replace('/', '-', trim('25/02/2013')))); 为我工作。
    • 是的,当我手动给出日期时,它对我有用。请注意,我正在从 excel 表中读取日期。所以结果不是 25/02/2013,而是 'TueTue/FebFeb/2013201320132013'
    【解决方案3】:

    PHP 有一个DateTime::createFromFormat() function,它会做你想做的事。

    $date = DateTime::createFromFormat('d/m/Y', '25/02/2013');  //import with the specified format...
    echo $date->format('Y-m-d');   //...and now you can output it in whatever format you want.
    

    【讨论】:

      【解决方案4】:

      PHPExcel 中有两个不同的函数来获取字段的值。

      $value  = $objPHPExcel->setActiveSheetIndex(0)->getCell('B12')->getValue();
      $value2 = $objPHPExcel->setActiveSheetIndex(0)->getCell('B12')->getCalculatedValue();
      

      我不确定您正在使用的 phpExcelReader 库中是否也存在不同的函数,但值得研究一下。

      进一步看,这是在文档中:

      Accessing Cell Values
      
      It is recommended that the public API functions be used to access data rather than relying on the underlying data structure, which may change between releases.
      
      Retrieve the formatted value of a cell (what is displayed by Excel) on the first (or only) worksheet:
      
          $data->val($row,$col) 
      
      You can also use column names rather than numbers:
      
          $data->val(10,'AZ') 
      
      Access data on a different sheet:
      
          $data->val($row,$col,$sheet_index) 
      

      所以尝试使用 val() 函数来获取值,而不是您使用的方法。

      【讨论】:

        【解决方案5】:

        Spreadsheet_Excel_Reader 类

        替换这个函数:

        function createDate($numValue) {
                if ($numValue > 1) {
                    $utcDays = $numValue - ($this->nineteenFour ? SPREADSHEET_EXCEL_READER_UTCOFFSETDAYS1904 : SPREADSHEET_EXCEL_READER_UTCOFFSETDAYS);
                    $utcValue = round(($utcDays) * SPREADSHEET_EXCEL_READER_MSINADAY);
        
                    $this->curformat = strtolower($this->curformat);
                    //echo $this->curformat; echo "<br>";
                    if ($this->curformat == 'mm/dd/yyyy' || $this->curformat == 'i/dd/yyyy') {
                        $this->curformat = 'Y-m-d';
                    }
        
                    $string = date($this->curformat, $utcValue);
                    $raw = $utcValue;
                } else {
                    $raw = $numValue;
                    $hours = floor($numValue * 24);
                    $mins = floor($numValue * 24 * 60) - $hours * 60;
                    $secs = floor($numValue * SPREADSHEET_EXCEL_READER_MSINADAY) - $hours * 60 * 60 - $mins * 60;
                    $string = date($this->curformat, mktime($hours, $mins, $secs));
                }
        
                return array($string, $raw);
            }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2018-05-25
          • 2010-12-23
          • 1970-01-01
          • 1970-01-01
          • 2023-03-08
          • 2015-10-26
          • 1970-01-01
          相关资源
          最近更新 更多