Format excel date cells as date for import/export
This commit is contained in:
@@ -9,6 +9,7 @@ use Maatwebsite\Excel\Concerns\ShouldAutoSize;
|
||||
use Maatwebsite\Excel\Concerns\WithHeadings;
|
||||
use Maatwebsite\Excel\Concerns\WithMapping;
|
||||
use Maatwebsite\Excel\Concerns\WithTitle;
|
||||
use PhpOffice\PhpSpreadsheet\Shared\Date as ExcelDate;
|
||||
|
||||
abstract class Export implements FromCollection, ShouldAutoSize, WithHeadings, WithMapping, WithTitle
|
||||
{
|
||||
@@ -41,7 +42,7 @@ abstract class Export implements FromCollection, ShouldAutoSize, WithHeadings, W
|
||||
$value = $model->$field;
|
||||
|
||||
if (in_array($field, $date_fields)) {
|
||||
$value = Date::parse($value)->format('Y-m-d');
|
||||
$value = ExcelDate::PHPToExcel(Date::parse($value)->format('Y-m-d'));
|
||||
}
|
||||
|
||||
// Prevent CSV injection https://security.stackexchange.com/a/190848
|
||||
|
||||
@@ -17,6 +17,7 @@ use Maatwebsite\Excel\Concerns\WithMapping;
|
||||
use Maatwebsite\Excel\Concerns\WithValidation;
|
||||
use Maatwebsite\Excel\Validators\Failure;
|
||||
use Illuminate\Support\Facades\Validator;
|
||||
use PhpOffice\PhpSpreadsheet\Shared\Date as ExcelDate;
|
||||
|
||||
abstract class Import implements ToModel, SkipsOnError, SkipsOnFailure, WithBatchInserts, WithChunkReading, WithHeadingRow, WithMapping, WithValidation
|
||||
{
|
||||
@@ -44,7 +45,7 @@ abstract class Import implements ToModel, SkipsOnError, SkipsOnFailure, WithBatc
|
||||
continue;
|
||||
}
|
||||
|
||||
$row[$date_field] = Date::parse($row[$date_field])->format('Y-m-d H:i:s');
|
||||
$row[$date_field] = Date::parse(ExcelDate::excelToDateTimeObject($row[$date_field]))->format('Y-m-d H:i:s');
|
||||
}
|
||||
|
||||
return $row;
|
||||
|
||||
Reference in New Issue
Block a user