It takes up little space, timestamp occupies 4 bytes, and datetime occupies 8 bytes;
Contains time zone information. If the time zone is modified in the future, the time represented by timestamp will change with the time zone, but datetime will not.
Disadvantages of timestamp:
can only represent the time from 1970 to 2038, and datetime is not subject to this restriction.
Performance comparison cannot be generalized. Although timestamp is a pure number, the calculation is faster, but it is not readable by humans and must be converted by functions. Datetime is a string type and is directly readable by the human eye. Although it is necessary to calculate addition, subtraction, multiplication and division. After a transformation.
For small applications, there is no need to consider space or performance. The time zone is all Beijing time. Just use datetime directly, and there is no need to worry.
Conclusion: Always use datetime to represent time, never use timestamp unless you have a clear reason or you know what you are doing.
Both types store time. As for the difference in memory usage and efficiency, I won’t talk about it here. In general applications, this difference has no essential impact on your system, so my point of view is business-oriented. Compared with datetime and timestamp, a timestamp type field will automatically update the value of this field when the database record changes (update), while datetime needs to be manually set before it changes. Just know this difference when filtering from a business perspective. Generally, choose datetime for create time and timestamp for update time
Recommend the questioner to read this post, it summarizes it well. Choose the appropriate MySQL date and time type to store your time
Search first
The reply here is very detailed
Advantages of timestamp compared to datetime:
It takes up little space, timestamp occupies 4 bytes, and datetime occupies 8 bytes;
Contains time zone information. If the time zone is modified in the future, the time represented by timestamp will change with the time zone, but datetime will not.
Disadvantages of timestamp:
can only represent the time from 1970 to 2038, and datetime is not subject to this restriction.
Performance comparison cannot be generalized. Although timestamp is a pure number, the calculation is faster, but it is not readable by humans and must be converted by functions. Datetime is a string type and is directly readable by the human eye. Although it is necessary to calculate addition, subtraction, multiplication and division. After a transformation.
For small applications, there is no need to consider space or performance. The time zone is all Beijing time. Just use datetime directly, and there is no need to worry.
Conclusion: Always use datetime to represent time, never use timestamp unless you have a clear reason or you know what you are doing.
Both types store time. As for the difference in memory usage and efficiency, I won’t talk about it here. In general applications, this difference has no essential impact on your system, so my point of view is business-oriented.
Compared with datetime and timestamp, a timestamp type field will automatically update the value of this field when the database record changes (update), while datetime needs to be manually set before it changes.
Just know this difference when filtering from a business perspective. Generally, choose datetime for create time and timestamp for update time