Convert Date time to carbon Instance
Asked Answered
S

1

8

I'm using date time input field

<input type="datetime-local">

It returns a formatted date time yyyy-MM-ddThh:mm

Now i want to convert this format to Y-m-d H:i using php-carbon Package.

I have tried

 \Carbon\Carbon::createFromFormat(

    'yyyy-MM-ddTh:mm', $this->start_date

 )->format('Y-m-d H:i:s');

But it failed.

Sadye answered 30/10, 2019 at 10:26 Comment(5)
Have you tried specifying the import format like: 'Y-m-dTH:i', $this->start_date) ?Flamen
Yes i did but it also failed Saying The separation symbol could not be found Unexpected data found.Sadye
I think syntax error proper syntax Carbon::createFromFormat($format, $time, $tz);Underthecounter
Can you add a raw date from the $this->start_date var to the question?Flamen
$this->start_date it came from FormRequest class in laravel. This solution worked well \Carbon\Carbon::parse($this->start_date)->format('Y-m-d H:i'); Thanks for helping :) @FlamenSadye
M
18

you can use Carbon::parse for date parse.

\Carbon\Carbon::parse($this->start_date)->format('Y-m-d H:i');
Memphian answered 30/10, 2019 at 10:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.