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

AlexanderKim's avatar

How to save current date as unix timestamps in Postgres?

Now, i'm saving my date this way:

$link->created = Carbon::now();

But in postgre i'm getting '2019-08-29' kind of date, so i cannot reformat it on my frontend with hours, minutes, seconds. What's the correct way on saving current date with Carbon?

0 likes
9 replies
Sergiu17's avatar

What is the type of the column in your database?

Sergiu17's avatar
Sergiu17
Best Answer
Level 60

@alexanderkim well, the DATE type stores only DATE without time, you need to change your column data type in order to store the hours, minutes and seconds.

In MYSQL would be: from DATE type, to DATETIME type

AlexanderKim's avatar

Postgres is a bit different in this case, i've tried to save current date as Carbon::now()->timestamp, so the date would like unix timestamp 453412334123, but postgres won't save this in date, dateTime, timestamp field, throw ERROR 7 with overflow thing.

latz's avatar

Postgres uses the datatype timestamp (date + time). If you want to work with the unix timestamp (1567166908) take an another data type.

Sinnbeck's avatar

Can you show your migration for the table (if any exists).

AlexanderKim's avatar

@resin

Schema::create('links', function (Blueprint $table) {
    $table->string('id')->primary();
    $table->string('title', 300)->nullable();
    $table->dateTime('created');
});

Not using $table->timestamps() on purpose.

Sinnbeck's avatar

Seems that postgres does not have a DATETIME column type. You could make it an integer at save it as as an unix timestamp. And then add a mutator for the column to convert it to carbon automatically.

1 like

Please or to participate in this conversation.