Difference between timestamp() and dateTime() methods of the Blueprint class

14,906

Solution 1

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.

Solution 2

$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.

Solution 3

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.

Share:
14,906

Related videos on Youtube

Goms
Author by

Goms

Fan de l'univers wavob click de streaming et développeur web.

Updated on June 18, 2022

Comments

  • Goms
    Goms almost 2 years

    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?

  • Goms
    Goms over 6 years
    If I well understand, both functions are binded to MySQL if it is my DBMS ?
  • Grant
    Grant over 3 years
    So in 2038 we're gonna have a Y2K bug again?