Considering DATETIME over TIMESTAMP for created_at/updated_at? #50578
Replies: 5 comments 17 replies
-
Hi. I always use in my migrations
And use a boot function in the model to populate them. You are refering to which populates them automatically with timestamps? |
Beta Was this translation helpful? Give feedback.
-
I think that by the time we get to the year 2038 they have increased that limit and you have upgraded to the latest database version. So that is not really an argument. |
Beta Was this translation helpful? Give feedback.
-
The meaning of using The |
Beta Was this translation helpful? Give feedback.
-
I absolutely agree with the OP, Laravel should switch to DateTime columns. Otherwise, all laravel-based apps will begin failing in 2038. |
Beta Was this translation helpful? Give feedback.
-
By the time this becomes a problem, most database engines will move to 64-bit timestamps, if not before. PHP already uses 64-bit timestamps (if you run PHP 64-bit). This discussion is pointless. |
Beta Was this translation helpful? Give feedback.
-
In Laravel, the
created_at
andupdated_at
columns typically use the timestamp data type in MySQL. This approach might stem from earlier MySQL versions where onlyTIMESTAMPS
columns could automatically be set toCURRENT_TIMESTAMP
at the database level, unlikeDATETIME
columns. However, since MySQL 5.6.5,DATETIME
columns have gained the capability to also default toCURRENT_TIMESTAMP
.While
TIMESTAMP
andDATETIME
fields serve similar purposes, there are two key differences to consider:TIMESTAMP
fields are limited to dates up to the year 2038.TIMESTAMP
fields undergo automatic timezone conversion based on the database's timezone settings. For instance, if your database's timezone is set toSET time_zone = '+02:00';
and you attempt to set created_at to a specific time, the actual stored time in the database will be adjusted to UTC, effectively reducing it by two hours. This behavior could lead to unintended side effects if not properly understood.Given these distinctions, should we perhaps consider using
DATETIME
as the default column type forcreated_at
andupdated_at
columns to avoid these limitations and potential complications?Beta Was this translation helpful? Give feedback.
All reactions