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

5
  • to generate Excel what are the plugin you using ? Commented Feb 5, 2020 at 4:42
  • You are using from collection or query or what? Commented Feb 5, 2020 at 4:43
  • @Hamelraj use maatwebsite/laravel-excel Commented Feb 5, 2020 at 4:46
  • @MuhammadDyasYaskur I use query builder for execute stored procedure here is my code pastebin.com/pZrHekBC Commented Feb 5, 2020 at 4:46
  • Problem is here imageupload.net/image/format-excel-date.CLCLp Commented Feb 5, 2020 at 4:48

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

3 Comments

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
My update code pastebin.com/DEGHJmGg @MuhammadDyasYaskur
@TemmyAlex try Carbon::parse($row['waktu_awal'])

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.