How to make my own timestamp method in Laravel?

13,918

Solution 1

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...

  1. In your Schema (migration), use

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

    instead of

    $table->timestamps();
    
  2. Replace the timestamp() function in your models.

  3. 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 {

}

Solution 2

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');
    });
}

Solution 3

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?

Solution 4

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'];
}

Solution 5

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();
Share:
13,918
DNB5brims
Author by

DNB5brims

Updated on June 09, 2022

Comments

  • DNB5brims
    DNB5brims almost 2 years

    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.