php - What format does the default timestamps (create_at and updated_at) in laravel have when saved to db?
I am about to save an additional timestamp to db: last_sent
I want to save it in the same way as the default timestamps (created_at and updated_at) - meaning that I am going to migrate it like so:
$table->timestamp('last_sent')->nullable
(It will be null sometimes..)
My question is what format on the time value should I save to this column?
I found two options but am not sure what is correct:
Carbon\Carbon::now()->toDateTimeString();
or just
Carbon\Carbon::now();
What is the correct way to save the date and time to get it like default timestamps?
Answer
Solution:
i think this is a Mysql matter ...
according to mysqltutorial
The MySQL TIMESTAMP is a temporal data type that holds the combination of date and time. The format of a TIMESTAMP is YYYY-MM-DD HH:MM:SS which is fixed at 19 characters.
so, the default is YYYY-MM-DD HH:MM:SS
and this is how time Stamp stored in db ...
and if you find out what $table->timestamps(); do it 's :
public function timestamps($precision = 0)
{
$this->timestamp('created_at', $precision)->nullable();
$this->timestamp('updated_at', $precision)->nullable();
}
so: laravel keep Mysql format with precision=0 by default if you want to store your column like default laravel time stamp you could:
$this->timestamp('last_sent',0)->nullable();
you can ignore second parameter
for values to your column:
Carbon\Carbon::now()->toDateTimeString();
this won't do it because it's simple a string not a date
but this will be enough:
$value=Carbon\Carbon::now();
in spite of that $value will hold four number for milliseconds those number will be ignored in db because of the (0 precision) in our migration ...
Answer
Solution:
Normally, it is in YYYY-MM-DD Hour:Min:Sec format. To make you clear, I will add a screenshot for this.
I hope it will help you.
Source