[英]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.