In MySQL, there are many situations that we need to use date functions, not only the transformation from timestamp to date, but also the comparision between different dates. Therefore, it is necessary for me to records some common date functions and consolidate my knowledge in this apsect.
Before starting this article, I would like to say some differences among three general time type in MySQL, date, datetime and timestamp.
- The format of date type is “YYYY-MM-DD”, so the date type can be accurate to ‘date’. In contrast, datetime type and timestamp type can be accurate to second.
- The range that timestamp covers is from 1970-01-01 00:00:01 to 2038-01-19 03:14:07. And the range for datetime type is more larger than for timestamp. It can express the time from 1000-01-01 00:00:00 to 9999-12-31 23:59:59.
- The time in timestamp can be converted automatically based on the timezone of the database, because in database it stores as UTC time zone, and when getting it from database, it will be converted based on UTC. The time in datetime is stable and cannot change as the time zone changes.
- Timestamp needs to use 4 bytes to store, datetime needs 8 bytes, and date uses 3 bytes.