Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel timestamp saving time as unix timestamp

Tags:

laravel

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!

like image 605
Ludwig Avatar asked Dec 15 '22 08:12

Ludwig


1 Answers

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

like image 194
Alexey Mezenin Avatar answered Dec 30 '22 04:12

Alexey Mezenin