Skip to content
This repository has been archived by the owner on May 26, 2022. It is now read-only.

Doing cell->getValue() on loop stops when the value is a DateTime Object #883

Open
Zeit42 opened this issue Apr 11, 2022 · 0 comments
Open

Comments

@Zeit42
Copy link

Zeit42 commented Apr 11, 2022

I have an excel file I'm trying to loop through. So far, I'm able to navigate to the worksheet I have to work on. I'm currently trying to loop through the data rows I have, and so far, this works for me:

    foreach ($sheet->getRowIterator() as $row) {
        $cells = $row->getCells();
        foreach ($cells as $cell){
            $cell_value = $cell->getValue();
            echo "Cell Value = $cell_value<br>\n";
        }
    }

For the most part, this works a treat because most of the values are strings or numbers.

However, I've noticed that it doesn't work for date.

For example, I used print_r on the $cells variable just to double check and here's what I got:

    [4] => Box\Spout\Common\Entity\Cell Object
        (
            [value:protected] => DateTime Object
                (
                    [date] => 1963-05-13 00:00:00.000000
                    [timezone_type] => 3
                    [timezone] => UTC
                )
         }

The value is being read properly but as opposed to the other data rows I have, the value is a datetime object and not a string, and this presents an error.

I saw the setShouldFormatDate property in the documentation. While I'm able to use this with getValue() to get the formatted value in Excel (usually a dd-MMM-yy format like 11 APR 22), I'd like to be able to get this in the yyyy-mm-dd format.

Is there a way to do this within the library? Or do I just get the formatted value and then use PHP's built in function to convert the formatted date to my desired format?

Thank you.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant