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

Presto's avatar

Formatting User Selected Date & Time With Carbon

Hey,

I'm building form that lets the user set a due date with a time, I'm using Bootstrap 3 Datepicker v4for the front end date picker, when the date / time is selected it is formatted like so "m/d/Y H:g A" so it looks like this in the field "10/16/2015 9:00 AM" I need to convert this to a timestamp before saving it to the database. How do you go about this, should I do this in my store function within the controller or in the model?

Here is the code I'm trying to use within my controller but I don't think I'm using Carbon correctly as its saving the date correctly but the time is not, i.e. its saved like "2015-10-16 00:00:00", without the time.

$request['due_at'] = Carbon::createFromFormat('m/d/Y H:g A', $request->due_at)->toDateTimeString();

What would you suggest?

0 likes
7 replies
jlrdw's avatar

Couldn't you just store it as a datetime field?

Presto's avatar

I have the due_at field set as timestamp in the database.

Ps. if I don't do anything to the date before saving it to the database the date is stored like 0000-00-00 00:00:00

jlrdw's avatar

If it's not converted to that, could you try a datetime field and test if that works?

Presto's avatar

Thank you guys so much, both the incorrect format that @thomaskim pointed out, and the incorrect column type that @jlrdw pointed out fixed the issue, I wish you could select more then one winning answer.

Please or to participate in this conversation.