Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

gidaban79's avatar

Carbon and Conversion date

Hello all,

I hope You are well :)

in my controller i have code for store informations from input, is date in format d/m/Y example 29/05/2017

and now i use code:

$post->featured_to = $request->featured ? Carbon::createFromFormat('d/m/Y', $request->featured_to)->toDateTimeString() : NULL;

Date is convert, but i have to convert date until midnight like '2017-05-31 00:00:00' but date is converted until hour what time i do convert,

Any ideas how to fix it?

0 likes
5 replies
Snapey's avatar

It is not at all clear what you are trying to achieve.

gidaban79's avatar

Sorry for late reply i was busy

input form

<input type="text" name="featured_to" />

just example.

And now user picking date by "Bootstrap Datepicker", after it field is filled "16/06/2017" -"dd/mm/yyyy"

After send form i catch it "$request->published_at"n still is only date, next i want to use "Carbon" for convert it

Carbon::createFromFormat('d/m/Y H:i:s',$request->published_at)->toDateTimeString()

after it i getting date in correct format, time not date, time is equal to time send of form, but i need date with time in midnight, 2017-06-16 00:00:00

Snapey's avatar
Carbon::createFromFormat('d/m/Y H:i:s', $request->published_at . '00:00:00');

There are lots of ways. One is above, concatenate the time onto the end of the published_at. It does depend though what format you return from your form?

Then add the field to the model $dates array then you can just pass it to the model without converting it back to a string

Cronix's avatar

bootstrap datepicker, as with most datepickers, allows you to set the date format so it will already be in that format (yyyy-mm-dd h:i:s) when the form is sent.

Please or to participate in this conversation.