Typically, the Laravel platform have a $table->timestamps();
in migration..., it generate two datetime
fields,
But I would like to implement my own timestamps or, maybe call unix_timestamps()
. I would like to have two fields named created_at
, and updated_at
, and which store the unix timestamps, how can I implement it? Thanks.

- 29,344
- 50
- 131
- 195
-
I'm assuming you want the same features as with $table->timestamp(), i.e. auto updating timestamps? Otherwise $table->integer('created_at') should work. – Karl Laurentius Roos Apr 05 '13 at 12:36
5 Answers
You don't have to use Laravel's timestamp helpers, but they are convenient. There are some good ways of working with string timestamps now too, including PHP's DateTime class. But I digress, to use unix timestamps...
In your Schema (migration), use
$table->integer('created_at'); $table->integer('updated_at');
instead of
$table->timestamps();
Replace the
timestamp()
function in your models.- Keep
$timestamps = true
in your models.
Here's an example base model which you could use, and extend instead of Eloquent on your models:
// models/basemodel.php
class BaseModel extends Eloquent {
/**
* Indicates if the model has update and creation timestamps.
*
* @var bool
*/
public static $timestamps = true;
/**
* Set the update and creation timestamps on the model.
*/
public function timestamp()
{
$this->updated_at = time();
if ( ! $this->exists) $this->created_at = $this->updated_at;
}
}
// models/thing.php
class Thing extends BaseModel {
}

- 20,000
- 3
- 33
- 46
For Laravel 4:
- override the freshTimestamp() method in your Eloquent model
- use integers in migration file in stead of timestamps
models/product.php
class Product extends Eloquent {
protected $table = 'products';
public function freshTimestamp()
{
return time();
}
}
Laravel 4 also mutates all dates/timestamps to Carbon instances (Documented here)
Which means you also need to overwrite the getDates()
method in order to prevent Carbon ruining your timestamp before insertion.
public function getDates()
{
return array();
}
database/migrations/2013_04_20_125823_create_products_table.php :
public function up()
{
Schema::create('products', function(Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->integer('created_at');
$table->integer('updated_at');
});
}

- 25,669
- 10
- 69
- 108

- 2,167
- 2
- 20
- 29
-
This method worked for me, however, on related tables I had to disabled the "touches" property because I was getting an InvalidArgumentException when running `db:seed`. I don't have the time to look into exactly why this is at the moment. – Ben Harold Feb 03 '14 at 22:14
I fear that you'll need some ugly hacks in order to rewrite the timestamps()
function, and I'm sure that's a bad idea.
If you need your own format, simply define a new column. There is even a timestamp column in Laravel's schema builder (see here for a full list of available formats):
$table->timestamp('added_on');
You would however need to define default values and/or ON UPDATE
by yourself, or you could use triggers. But in the end you're probably best off sticking to Laravel's timestamps()
, because it will take care of everything automatically. Why would you need anything else?

- 1,593
- 2
- 18
- 34
I had this same requirement and figured out a solution that may work for you too. I posted a repo of how I did it on Github: Laravel Integer SQL Dates <== check it out for more details, but here's the gist of it:
class Base extends Eloquent {
public function freshTimestamp()
{
return time(); // (int) instead of '2000-00-00 00:00:00'
}
public function fromDateTime($value)
{
return $value; // Don't mutate our (int) on INSERT!
}
// Uncomment, if you don't want Carbon API on SELECTs
// protected function asDateTime($value)
// {
// return $value;
// }
public function getDateFormat()
{
return 'U'; // PHP date() Seconds since the Unix Epoch
}
}
class User extends Base {
protected $table = 'users';
protected $fillable = ['email'];
}

- 2,801
- 2
- 18
- 22
Just create a migrations/model field that is a timestamp type, then in your controller, fill it with current time using this
$myField = new \DateTime();

- 1,087
- 11
- 15