Ludwig
Ludwig

Reputation: 1781

Laravel timestamp saving time as unix timestamp

I was wondering what would be the easiest way to change in laravel to save timestamps in database as unix valid timestamp?

Thanks in advance!

Upvotes: 7

Views: 15796

Answers (1)

Alexey Mezenin
Alexey Mezenin

Reputation: 163748

Please read about Laravel date mutators: https://laravel.com/docs/5.1/eloquent-mutators#date-mutators

By default, timestamps are formatted as 'Y-m-d H:i:s'. If you need to customize the timestamp format, set the $dateFormat property on your model. This property determines how date attributes are stored in the database, as well as their format when the model is serialized to an array or JSON

Also, you can override getDateFormat():

protected function getDateFormat()
{
    return 'U';
}

And use this in your migration files:

$table->integer('updated_at');
$table->integer('created_at');

And, if you use soft deletes:

$table->integer('deleted_at');

https://laravel.com/docs/4.2/eloquent#timestamps

Upvotes: 14

Related Questions