Reputation: 2644
In Laravel, the Illuminate\Database\Schema\Blueprint
class has two methods that I would like to know concretely the difference between them.
$table->dateTime()
and
$table->timestamp()
Both store a date in the same way visibly. Someone to enlighten me?
Upvotes: 31
Views: 27264
Reputation: 6125
$table->dateTime()
Create a new date-time column on the table. While on the other hand,
$table->timestamp()
Create a new timestamp column on the table.
If you have a problem identifying the difference between timestamp and datetime,
DATETIME represents a date (as found in a calendar) and a time (as can be observed on a wall clock)
And,
TIMESTAMP represents a well defined point in time. This could be very important if your application handles time zones. How long ago was '2010-09-01 16:31:00'? It depends on what timezone you're in.
Also, you can refer to BluePrint Documentation for any inconveniences.
Upvotes: 10
Reputation: 445
timestamp
and dateTime
store a date (YYYY-MM-DD) and time (HH:MM:SS) together in a single field i.e. YYYY-MM-DD HH:MM:SS.
The difference between the two is that timestamp
can use CURRENT_TIMESTAMP
as its value, whenever the database record is updated.
timestamp
has a limit of 1970-01-01 00:00:01 UTC to 2038-01-19 03:14:07 UTC dateTime
has a range of 1000-01-01 00:00:00 to 9999-12-31 23:59:59 UTC
timestamps
doesn't take an argument, it's a shortcut to add the created_at
and updated_at
timestamp fields to your database.
Upvotes: 8
Reputation: 1140
So the secret to this is understanding what exactly each does.
The dateTime()
and timestamp()
functions here in Laravel use different table columns.
dateTime()
uses DATETIME as the DB column type.
timestamp()
uses TIMESTAMP as the DB column type.
DATETIME
and TIMESTAMP
have a lot of similarities but the difference itself lies outside Laravel and more in MySQL.
Their major difference is the range. For DateTime its up to year 9999
while for timestamp its only up to year 2038
. Other differences include the amount of bytes needed to store each.
I found a nice article that clearly states the similarities and differences of both here http://www.c-sharpcorner.com/article/difference-between-mysql-datetime-and-timestamp-datatypes/
Hope this helps.
Upvotes: 56