Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Colin_Laws's avatar

Can Eloquent accept Unix time for date fields?

I am wondering if I can configure anything on my models or environment to allow setting a date field to an integer, and having Eloquent automatically interpret this as Unix time.

At the moment, it's a bit painful to use Carbon::createFromTimestamp($unixTime) everywhere. I would prefer just being able to set my date fields to Unix time, save, and have Eloquent handle the casting.

To be clear, I want the database type to be Timestamp, and be able to cast Unix time to a timestamp. I am seeing answers on other sites where users want to save Unix time in the database. I know how to achieve that functionality.

0 likes
6 replies
Colin_Laws's avatar

Thank you so much for this. So I have to directly specify on the model which fields are dates. I knew I could do this, because I've done it before. It was just hard to dig up the information again.

:)

1 like
Sti3bas's avatar
Sti3bas
Best Answer
Level 53

Migration:

Schema::create('tests', function (Blueprint $table) {
   //...
   $table->timestamp('test_date')->nullable();
});

Model:

class Test extends Model
{
   //...
   protected $dates = [
       'test_date'
   ];
}

Tinker:

>>> $model = \App\Test::first();
=> App\Test {#3042
      //...
      test_date: null,
   }
>>> $model->test_date = 1579183788;
=> 1579183788
>>> $model->save();
=> true
>>> $model->fresh();
=> App\Test {#3027
      //...
      test_date: "2020-01-16 14:09:48",
   }
>>>
1 like
fylzero's avatar

@colin_laws It sounds like you are storing the timestamp and unix time, correct? It might be easier just to use timestamps as is and create an accessor when needed to convert the Unix time when being displayed.

https://laravel.com/docs/6.x/eloquent-mutators#accessors-and-mutators

Obviously using Carbon now() for timestamp storage and using an accessor would be much simpler and keep you from having to juggle all of this everytime.

2 likes
Colin_Laws's avatar

I am storing the dates as a Timestamp. There are just situations where I have Unix time, and it's annoying having to cast it everywhere to a Timestamp to push it to my models.

The previous answer I accepted was what I needed. This functionality is already built into Laravel for date mutators.

1 like
jlrdw's avatar

I have never heard of using a timestamp for a date field, sorry makes no sense.

Colin_Laws's avatar

Alright, well let me break it down for you a little more.

Let's say you have a user object with a field called 'expired'. In the database, the field of 'expired' is of type Timestamp.

Simple, right? We have a column on the users table of type Timestamp.

Let's say, for instance, you integrate with a host of other systems (which I do). These systems don't all represent time the same way when communicating with my application. Some may send Unix time (1579196817), ISO 8601 ( '2020-01-16T17:46:57+00:00'), RFC 2822 ('Thursday, 16-Jan-20 17:46:57 UTC'), so on and so forth, you get the idea.

Let's say I am updating the Timestamp column 'expired' on my users table again.

    // This would be great to do
    $user->expired = 1579196817;
    $user->save();

It turns out that this is actually functionality already built into Laravel! Wohoo! Mutators are great. This is what I was describing in my original post. I want a mutator to automatically cast any type of date I pass to the field on the model, that way I'm not doing Carbon::createFromTimestamp() or Carbon::parse() all over my project. Instead, let's do it in one spot, and make it easy to just save dates directly to the model, and back it with a Timestamp.

\App\Models\User.php

    // Marvelous. All we do is let Eloquent know in our model that we have an extra date field.
    protected $dates = [
        'expired'
    ];

Unless of course, you mean that the $dates field is very strict in its definition of 'date' and that I can't use timestamps here. In which case, shouldn't there be a $timestamps field as well? I know the difference between a date and a timestamp, it's pretty obvious.

Please or to participate in this conversation.