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

postitief's avatar

Carbon date in database datetime format

Hello,

I was wondering if Carbon has a function to format the time as a datetime (yyyy-mm-dd hh:mm:ss) format for MySQL database. Searching for this didn't give me anything usefull.

If I now add a date to a database, I'm doing this with the default php date function:

$model = new MyModel;
$model->date = date("Y-m-d H:m:s");
$model->save();

This works like a charm, but wouldn't it be nice if I can leave the format out of it. Because a typographical error is made quickly.

So something like this would be nice:

$model = new MyModel;
$model->date = Carbon::now()->formatDateTime();
$model->save();

I can't find this. But probably I'm searching for the wrong thing, or this is just not in Carbon.

0 likes
3 replies
Red's avatar
Red
Best Answer
Level 4

You could use Carbon directly.

$model = new Model;
$model->date = Carbon::now();
$model->save();

You should then add a property to your model:

protected $dates = ['date'];
7 likes

Please or to participate in this conversation.