arogachev / yii2-excel

ActiveRecord import and export based on PHPExcel for Yii 2 framework
Other
64 stars 25 forks source link

Incorrect date value #30

Open ignatenkovnikita opened 8 years ago

ignatenkovnikita commented 8 years ago

If value in cell "30.09.2015", i get value "42277", beacause format cell is date. How detect date format?

ignatenkovnikita commented 8 years ago

I find solution File basic\Attribute modify method replaceValue after

$value = $this->cell->getValue();

add row ot check if value is date

if(PHPExcel_Shared_Date::isDateTime($this->cell)) {
            $value = PHPExcel_Shared_Date::ExcelToPHP($value);
        }
arogachev commented 8 years ago

I handled it with helper method:

use DateTime;
use PHPExcel_Shared_Date;

/**
 * @param $excelDateTime
 * @return string
 */
protected static function convertDateTime($excelDateTime)
{
    $dateTime = (new DateTime)->setTimestamp(PHPExcel_Shared_Date::ExcelToPHP($excelDateTime));

    return $dateTime->modify("- {$dateTime->getOffset()} seconds")->format('Y-m-d H:i');
}

Then in standardAttributesConfig I just call it like that:

[
    'name' => 'available_from',
    'valueReplacement' => function ($value) {
        return self::convertDateTime($value);
    },
],
arogachev commented 8 years ago

But yes, it will be better if an extension provides better abstracton for that. Will think how to add this feature.