Skip to content

Commit 07db01c

Browse files
author
Patrick Brouwers
committed
Fix for date formats
1 parent 4b73bad commit 07db01c

File tree

1 file changed

+30
-8
lines changed

1 file changed

+30
-8
lines changed

src/Maatwebsite/Excel/Excel.php

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
use Config;
44
use Maatwebsite\Excel\Readers\HTML_reader;
55
use \PHPExcel_Shared_Date;
6+
use \PHPExcel_Style_NumberFormat;
67

78
/**
89
* Laravel wrapper for PHPEXcel
@@ -813,19 +814,40 @@ private function parseCells()
813814
$index = $this->labels[$i];
814815
}
815816

816-
// If the cell is a date time and we want to parse them
817-
if($this->formatDates !== false && PHPExcel_Shared_Date::isDateTime($this->cell))
817+
// If the cell is a date time
818+
if(PHPExcel_Shared_Date::isDateTime($this->cell))
818819
{
819-
// Convert excel time to php date object
820-
$value = PHPExcel_Shared_Date::ExcelToPHPObject($this->cell->getCalculatedValue());
821820

822-
// Format the date
823-
$value = $value->format($this->dateFormat);
821+
// Check if we want to parse the dates
822+
if ($this->formatDates !== false)
823+
{
824+
825+
// Convert excel time to php date object
826+
$value = PHPExcel_Shared_Date::ExcelToPHPObject($this->cell->getCalculatedValue());
827+
828+
// Format the date
829+
$value = $value->format($this->dateFormat);
830+
831+
// Use carbon to parse the time
832+
if($this->useCarbon)
833+
{
834+
$value = \Carbon::parse($value)->{$this->carbonMethod}();
835+
}
824836

825-
if($this->useCarbon)
837+
}
838+
else
826839
{
827-
$value = \Carbon::parse($value)->{$this->carbonMethod}();
840+
// Format the date to a formatted string
841+
$value = (string) PHPExcel_Style_NumberFormat::toFormattedString(
842+
$this->cell->getCalculatedValue(),
843+
$this->cell->getWorksheet()->getParent()
844+
->getCellXfByIndex($this->cell->getXfIndex())
845+
->getNumberFormat()
846+
->getFormatCode()
847+
);
828848
}
849+
850+
829851
}
830852

831853
// Check if we want calculated values or not

0 commit comments

Comments
 (0)