-3

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.

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'));
}

enter image description here

  • 2
    Don't truncate or paraphrase error messages - it's really unhelpful for those trying to diagnose the issue. It's like telling your doctor "it hurts" but then not pointing to a place on your body. [edit] your question to show the exact, full error message and also tell us what line of your code threw it. Also please show an example of the data you're trying to process when this happens. We need a [mre] of the issue, not some unexplained code and half an error message. See also [ask]. Remember we are not looking over your shoulder. Thanks :-) – ADyson Apr 26 '22 at 11:12
  • Apart from the problem you described why did you repeat the `foreach` twice instead of using the first for both? – Simone Rossaini Apr 26 '22 at 11:13
  • Also please read the tag descriptions before you use them. The description of the "web" tag starts with "Do not use this tag"....! Possibly a phpspreadsheet tag would be useful instead, and maybe datetime. You can re-tag it as well when you edit it. – ADyson Apr 26 '22 at 11:13
  • 1
    Don't just tell us the line number for an error message. Show us, with a comment or something, the line causing the crash. And, if you can rig up an IDE with a debugger so you can step through this sort of thing and check that your actual data matches your expectations. – O. Jones Apr 26 '22 at 11:21
  • Please [edit] your question to show us the text contents of `$row[$column2]`, or maybe the output of `print_r( $row[$column2] )`. – O. Jones Apr 26 '22 at 11:48
  • Please provide enough code so others can better understand or reproduce the problem. – Community Apr 26 '22 at 19:28

1 Answers1

1

I guess this is the offending line in your code; it's the only line using a ->format() method.

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. That makes ::excelToDateTimeObject() return false, letting you know you have an error. Then you try to invoke the format method like this (false)->format( 'Y' ). That's why php threw the error message you showed us.

And, you don't do anything with the output of json_encode() so even if everything worked it would get lost.

O. Jones
  • 103,626
  • 17
  • 118
  • 172
  • Do you know a way to format the date?When I try to show the $row[$column2] is provides the correct dates,but I don't know why it always shows 1970 when trying to format. – Madalinaxyz Apr 26 '22 at 11:40