0

Hi I have a problem for date time format

when I set a string to date like this

$row['waktu_awal'] = date('Y-m-d H:i:s', strtotime($discharge->waktu_awal));

Format text/string change to date but in excel format not change to date

like this picture

Excel

Any solution to this case?

Thanks

Muhammad Dyas Yaskur
  • 6,914
  • 10
  • 48
  • 73
Temmy Alex
  • 43
  • 1
  • 9

1 Answers1

1

Try

$row['waktu_awal'] = date('Y-m-d H:i:s', strtotime($discharge->waktu_awal));
$row['waktu_awal'] = Date::dateTimeToExcel(Carbon::parse($row['waktu_awal']));

);

and you can format from the column use:

public function columnFormats(): array
{
    return [
        'B' => NumberFormat::FORMAT_DATE_DATETIME,
    ];
}

reference: https://docs.laravel-excel.com/3.1/exports/column-formatting.html

Muhammad Dyas Yaskur
  • 6,914
  • 10
  • 48
  • 73
  • Hi i have error when try this $row['waktu_awal'] = date('Y-m-d H:i:s', strtotime($discharge->waktu_awal)); $row['waktu_awal'] = Date::dateTimeToExcel($row['waktu_awal']); Argument 1 passed to PhpOffice\PhpSpreadsheet\Shared\Date::dateTimeToExcel() must implement interface DateTimeInterface, string given, called in @MuhammadDyasYaskur – Temmy Alex Feb 05 '20 at 04:58
  • My update code https://pastebin.com/DEGHJmGg @MuhammadDyasYaskur – Temmy Alex Feb 05 '20 at 04:59
  • @TemmyAlex try `Carbon::parse($row['waktu_awal'])` – Muhammad Dyas Yaskur Feb 05 '20 at 06:29