Search code examples
phplaravelphp-carbon

Add hour and min to date?


I have date comes from request for example 20-08-2021 I need to put the hour & min with it and be something like that 2021-20-08 14:18:12 to store in database

I tried this but it stores in DB like date of now!

$data['expires_at'] = $request->expires_at . '-' . now()->format('h:m');

Solution

  • Just convert the date to Carbon with createFromFormat so that it sets the time, then you can use the formatter functions

    $data['expires_at'] = Carbon::createFromFormat('d-m-Y', $request->expires_at)->toDateTimeString();