简体   繁体   English

致命错误:未捕获的错误:在第 58 行调用 bool 上的成员函数 format()

[英]Fatal error: Uncaught Error: Call to a member function format() on bool on line 58

I must retrieve data from a CSV file and to show the month and year from a date written in string format.I run the code and it shows the A non well formed numeric value encountered error.I also tried other ways to convert the date and I get the year 1900 for each date.我必须从 CSV 文件中检索数据并显示以字符串格式编写的日期的月份和年份。我运行代码,它显示 A non well-formed numeric value烬遇到错误。我还尝试了其他方法来转换日期和我得到每个日期的 1900 年。

use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
require('php_library/spreadsheet-reader-master/php-excel-reader/excel_reader2.php');

require('php_library/spreadsheet-reader-master/SpreadsheetReader.php');


if(isset($_FILES["filename"]))
{
 $file = $_FILES["filename"]["tmp_name"];
 $file_open = fopen($file,"r");
$reader = new \PhpOffice\PhpSpreadsheet\Reader\Csv();
$spreadsheet = $reader->load($file);
$sheetData = $spreadsheet->getActiveSheet()->toArray();
 foreach ($sheetData as $i=>$Row)
    {
        foreach($Row as $j=>$column)
        {
            // echo $Row[$j].", ";
            if($i == 0)
            {
            if($Row[$j] == "Codice cliente")
                $column1 = $j;
            if($Row[$j] == "Data emissione")
            {
                $column2 = $j;
                // echo $j;
            }
        }
            
        }
      

    }
foreach ($sheetData as $i=>$row)

    {  
        if($i!=0){
      json_encode(\PhpOffice\PhpSpreadsheet\Shared\Date::excelToDateTimeObject($row[$column2])->format('Y'));
}

在此处输入图像描述

I guess this is the offending line in your code;我想这是您代码中的违规行; it's the only line using a ->format() method.这是唯一使用->format()方法的行。

json_encode(\PhpOffice\PhpSpreadsheet\Shared\Date::excelToDateTimeObject($row[$column2])->format('Y'));

Let's try breaking it into multiple lines so it's readable.让我们试着把它分成多行以便它可读。

$excelDatestamp = $row[$column2];
$dto = \PhpOffice\PhpSpreadsheet\Shared\Date::excelToDateTimeObject( $excelDatestamp ); 
$year = $dto->format( 'Y' );
json_encode( $year );

It seems likely to me that the $excelDatestamp generated by this refactoring of your code is malformed.在我看来,由您的代码重构生成的$excelDatestamp很可能是格式错误的。 That makes ::excelToDateTimeObject() return false , letting you know you have an error.这使得::excelToDateTimeObject()返回false ,让您知道您有错误。 Then you try to invoke the format method like this (false)->format( 'Y' ) .然后你尝试像这样调用格式方法(false)->format( 'Y' ) That's why php threw the error message you showed us.这就是 php 抛出您向我们展示的错误消息的原因。

And, you don't do anything with the output of json_encode() so even if everything worked it would get lost.而且,您不会对json_encode()的输出做任何事情,所以即使一切正常,它也会丢失。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 致命错误:未捕获错误:在 bool 上调用成员函数 fetch() - Fatal error: Uncaught Error: Call to a member function fetch() on bool 致命错误:未被捕获的错误:在布尔上调用成员函数RowCount() - Fatal error: Uncaught Error: Call to a member function RowCount() on bool 致命错误:未捕获的错误:调用 bool 上的成员 function - Fatal error: Uncaught Error: Call to a member function on bool 致命错误:未捕获的错误:在 null 上调用成员函数 format() - Fatal error: Uncaught Error: Call to a member function format() on null 致命错误:未被捕获的错误:在字符串上调用成员函数format() - Fatal error: Uncaught Error: Call to a member function format() on string 致命错误:未捕获的错误:在第9行的布尔值上调用成员函数execute() - Fatal error: Uncaught Error: Call to a member function execute() on boolean on line 9 致命错误:未捕获的错误:调用成员函数 - Fatal error: Uncaught Error: Call to a member function 致命错误:未捕获错误:调用成员 function fetchAll() on bool php mvc pdo - Fatal error: Uncaught Error: Call to a member function fetchAll() on bool php mvc pdo 当我在 php 中执行查询时,我得到:致命错误:未捕获的错误:调用成员 function close() on bool in - When i execute a query in php, i get: Fatal error: Uncaught Error: Call to a member function close() on bool in “致命错误:未捕获的错误:调用成员 function bind_param() on bool”它说 - "Fatal error: Uncaught Error: Call to a member function bind_param() on bool" it says
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM