Skip to content
This repository has been archived by the owner on May 26, 2022. It is now read-only.
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

Description

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.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions