Choosing the Right Date/Time Storage Type in MySQL: DATETIME vs TIMESTAMP vs Unix Timestamp
This article explains why storing dates as strings is problematic, compares MySQL's DATETIME and TIMESTAMP types—including their storage size, range, and timezone behavior—provides practical SQL examples, discusses numeric Unix timestamps, and offers guidance on selecting the most suitable type for different scenarios.
In everyday software development, storing time is a fundamental requirement because timestamps are tightly coupled with business logic such as operation logs, financial transactions, travel schedules, and order times. Selecting the proper MySQL date‑time type is therefore critical for data accuracy and system stability.
Do Not Store Dates as Strings
Beginners often use VARCHAR to store values like 'YYYY‑MM‑DD HH:MM:SS' . This approach wastes storage space, makes comparisons inefficient (lexicographic ordering can produce incorrect results), prevents the use of built‑in date functions, and leads to poor index performance.
DATETIME vs TIMESTAMP
DATETIME stores the literal date‑time value without any timezone information. The value you insert is stored exactly as provided.
TIMESTAMP converts the input from the current session timezone to UTC for internal storage and converts it back to the session timezone on retrieval, making it suitable for multi‑timezone applications.
Key Comparison Dimensions
Space Efficiency : DATETIME uses 5‑8 bytes (depending on fractional seconds), while TIMESTAMP uses 4‑7 bytes.
Range : DATETIME covers 1000‑01‑01 to 9999‑12‑31; TIMESTAMP covers 1970‑01‑01 to 2038‑01‑19.
Timezone : DATETIME has none; TIMESTAMP handles timezone conversion automatically.
Example table creation and data insertion:
CREATE TABLE `time_zone_test` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`date_time` datetime DEFAULT NULL,
`time_stamp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;Insert a row (session timezone UTC+0):
INSERT INTO time_zone_test(date_time,time_stamp) VALUES (NOW(), NOW());Query in the same session:
SELECT date_time, time_stamp FROM time_zone_test;Result shows identical values because the session timezone matches UTC.
Change the session timezone to UTC+8 and query again:
SET time_zone = '+8:00'; # TIMESTAMP value is automatically converted to UTC+8
+---------------------+---------------------+
| date_time | time_stamp |
+---------------------+---------------------+
| 2020-01-11 09:53:32 | 2020-01-11 17:53:32 |
+---------------------+---------------------+MySQL Timezone Commands
# Show current session timezone
SELECT @@session.time_zone;
# Set session timezone
SET time_zone = 'Europe/Helsinki';
SET time_zone = '+00:00';
# Show global timezone
SELECT @@global.time_zone;
# Set global timezone
SET GLOBAL time_zone = '+8:00';
SET GLOBAL time_zone = 'Europe/Helsinki';Are Numeric Timestamps Better?
Many projects store Unix timestamps in INT or BIGINT . This offers the same advantages as TIMESTAMP for sorting and comparison, but sacrifices human readability.
Conversion examples:
SELECT UNIX_TIMESTAMP('2020-01-11 09:53:32');
-- Returns 1578707612
SELECT FROM_UNIXTIME(1578707612);
-- Returns 2020-01-11 09:53:32PostgreSQL Equivalents
PostgreSQL does not have a DATETIME type. Its TIMESTAMP WITHOUT TIME ZONE behaves like MySQL DATETIME , while TIMESTAMP WITH TIME ZONE (or TIMESTAMPTZ ) corresponds to MySQL TIMESTAMP and handles automatic UTC conversion.
Summary Table
Type
Storage
Format
Range
Timezone
DATETIME
5~8 bytes
YYYY‑MM‑DD hh:mm:ss[.fraction]
1000‑01‑01 … 9999‑12‑31
No
TIMESTAMP
4~7 bytes
YYYY‑MM‑DD hh:mm:ss[.fraction]
1970‑01‑01 … 2038‑01‑19
Yes
Numeric Timestamp
4 bytes
Integer (e.g., 1578707612)
After 1970‑01‑01
No
Selection Recommendations
Use TIMESTAMP when you need built‑in timezone handling and your dates fall before 2038.
Use DATETIME when you want full control over timezone logic or need to store dates beyond 2038.
Use a numeric Unix timestamp for maximum comparison performance and cross‑system portability, accepting reduced readability.
There is no one‑size‑fits‑all solution; choose the type that best matches your application’s requirements.
IT Services Circle
Delivering cutting-edge internet insights and practical learning resources. We're a passionate and principled IT media platform.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.