jeudi 3 octobre 2019

Laravel Excel convert change date format

I'm trying to convert date with laravel excel and its not working, below is my code :

<?php

namespace App\Exports;

use App\Project;

use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Concerns\WithMapping;
use Maatwebsite\Excel\Concerns\ShouldAutoSize;
use Maatwebsite\Excel\Concerns\WithTitle;
use Maatwebsite\Excel\Concerns\WithEvents;
use Maatwebsite\Excel\Events\AfterSheet;
use PhpOffice\PhpSpreadsheet\Style\NumberFormat;
use Maatwebsite\Excel\Concerns\WithColumnFormatting;

class ProjectExport implements FromCollection, WithHeadings, WithMapping, ShouldAutoSize, WithTitle, WithEvents, WithColumnFormatting
{
    public function collection()
    {
        return Project::with('developer_projects')->get();
    }

    // Select data from query and set its position
    public function map($project): array
    {
        return [
            $project->name,
            $project->created_at,
            $project->start_date,
            $project->expiry_date,
        ];
    }

    // Set Date Format
    public function columnFormats(): array
    {
        return [
            'B' => NumberFormat::FORMAT_DATE_DDMMYYYY,
            'C' => NumberFormat::FORMAT_DATE_DDMMYYYY,
            'D' => NumberFormat::FORMAT_DATE_DDMMYYYY,
        ];
    }
}

the start_date and expiry_date are date field and created_at is timestamp

and I wanted my date to look like this

dd/mm/YYYY hh:mm:ss



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire