Phpspreadsheet: PhpOffice\PhpSpreadsheet\Shared\Date::isDateTime not working

Created on 18 Jul 2019  路  1Comment  路  Source: PHPOffice/PhpSpreadsheet

This is:

- [X] a bug report
- [ ] a feature request
- [X] **not** a usage question (ask them on https://stackoverflow.com/questions/tagged/phpspreadsheet or https://gitter.im/PHPOffice/PhpSpreadsheet)

What is the expected behavior?

PhpOffice\PhpSpreadsheet\Shared\Date::isDateTime must return true for excel date cells

What is the current behavior?

PhpOffice\PhpSpreadsheet\Shared\Date::isDateTime return false for excel date cells

What are the steps to reproduce?

Sample: https://github.com/Mapteg34/date-test
In sample we have test.xlsx file (see in repo), with one column.
Cells with date in excel formatting displaying as date type (see screen in repo).
But when we check it with Date::isDateTime - got false... (see test.php)

Sample (test.php in repo):

<?php

use PhpOffice\PhpSpreadsheet\Reader\Xlsx;
use PhpOffice\PhpSpreadsheet\Shared\Date;

require 'vendor/autoload.php';

$reader = new Xlsx();
$reader->setReadDataOnly(true);
$spreadsheet = $reader->load("test.xlsx");
$activeSheet = $spreadsheet->getActiveSheet();

foreach (['A1', 'A2', 'A3'] as $pCoordinate) {
    $pCell =$activeSheet->getCell($pCoordinate);
    $isDateTime = Date::isDateTime($pCell);
    printf(
        '%s isDateTime: %s, value: %s'.PHP_EOL,
        $pCoordinate,
        $isDateTime ? 'Yes' : 'No',
        $pCell->getValue()
    );
}

Test:

mapt@mpc date-test % php -f test.php 
A1 isDateTime: No, value: 袛邪褌邪 褋芯蟹写邪薪懈褟
A2 isDateTime: No, value: 43628.818055556
A3 isDateTime: No, value: 29362.177083333

Which versions of PhpSpreadsheet and PHP are affected?

mapt@mpc date-test % php -v
PHP 7.3.5 (cli) (built: Jun  3 2019 10:11:50) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.3.5, Copyright (c) 1998-2018 Zend Technologies
    with Zend OPcache v7.3.5, Copyright (c) 1999-2018, by Zend Technologies
mapt@mpc date-test % composer show
markbaker/complex        1.4.7 PHP Class for working with complex numbers
markbaker/matrix         1.1.4 PHP Class for working with matrices
phpoffice/phpspreadsheet 1.8.2 PHPSpreadsheet - Read, Create and Write Spreadsheet documents in PHP - Spreadsheet engine
psr/simple-cache         1.0.1 Common interfaces for simple caching

Most helpful comment

In MS Excel, a date is simply a number with a format mask.

Your problem is

$reader->setReadDataOnly(true);

This line tells PHPSpreadsheet to read only the raw data, with no styling information, no format masks, which means that dates are simply floating point numbers, and there is nothing to identify them as dates.

Date::isDateTime($pCell);

reads the format mask for the cell, and uses that mask to determine if the value is formatted as a date or not. As you've explicitly disabled loading of format masks, PHSpreadsheet cannot identify a date from a non-existent mask.

>All comments

In MS Excel, a date is simply a number with a format mask.

Your problem is

$reader->setReadDataOnly(true);

This line tells PHPSpreadsheet to read only the raw data, with no styling information, no format masks, which means that dates are simply floating point numbers, and there is nothing to identify them as dates.

Date::isDateTime($pCell);

reads the format mask for the cell, and uses that mask to determine if the value is formatted as a date or not. As you've explicitly disabled loading of format masks, PHSpreadsheet cannot identify a date from a non-existent mask.

Was this page helpful?
0 / 5 - 0 ratings