7

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!

1

1 Answer 1

14

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

Sign up to request clarification or add additional context in comments.

7 Comments

Thank you for your answer! So, what would the property look like to be able to save timestamps as valid unix timestamps? I found this in documentation: protected $dateFormat = 'U'; but couldn't find what would the format be like for Unix timestamps?
Just use this in your model and Laravel will use UNix time format, like: 1414706400
But when I use this, in my database for every timestamp field I get 0000-00-00 00:00:00
Try to use ->table elements from my post in your migration. It uses timestamp() by default.
everything worked now, when I did as you said! Thank you!
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.