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

trevorpan's avatar

How to set migration for a unix timestamp (in addition to laravel's normal created_at)

 redeem_by optional

Unix timestamp specifying the last time at which the coupon can be redeemed. After the redeem_by date, the coupon can no longer be applied to new customers.

Stripe has a few attributes on their coupon object. Two are the created and redeem_by.

Is there a simple way to set the migration?

Something similar to:

$table->timestamps();

Thank you ~

0 likes
7 replies
DDSameera's avatar

You can try this if you want date time string: It Produces something like "2019-03-11 12:25:00"

use Carbon\Carbon; $current_date_time = Carbon::now()->toDateTimeString();

gitwithravish's avatar

Steps

  • Set column type as integer
  • Set accessor in eloquent to show you the dates as timestamp !

Migration

$table->integer('created');
$table->integer('reedem_by');

Model.php

protected function getDateFormat()
{
    return 'U';
}
trevorpan's avatar

Hi @ddsameera ,

In this particular case Stripe is looking for the unix epoch 017483748937289 style time.

gitwithravish's avatar

@trevorpan it will do the job for you. I have delt with the same situation in past. This is the easiest approach.

trevorpan's avatar
trevorpan
OP
Best Answer
Level 15

@gitwithravish

Thanks again for having a look. It got me thinking a little deeper. Hope you can use this:

error: Symfony\Component\ErrorHandler\Error\FatalError Access level to App\Models\Coupon::getDateFormat() must be public (as in class Illuminate\Database\Eloquent\Model)

I found this was less work and more direct:

//Controller
//validation

	$createdUnix = Carbon::now()->timestamp;

//then..

        $coupon = Coupon::create([
        ...
        'created' => $createdUnix,
	...
]);

Please or to participate in this conversation.