MySQL TIMESTAMP vs DATETIME vs BIGINT: Which Should You Choose?
Inserting a 2040 contract expiry date into a MySQL TIMESTAMP column throws an out-of-range error because that column stops working in January 2038. Choosing between TIMESTAMP, DATETIME, and BIGINT early prevents painful table migrations when your database grows.
Side by Side Comparison in MySQL 8
| Feature | TIMESTAMP | DATETIME | BIGINT (Epoch) |
|---|---|---|---|
| Storage Size | 4 bytes (+ fractional) | 5 bytes (+ fractional) | 8 bytes |
| Supported Date Range | 1970-01-01 to 2038-01-19 | 1000-01-01 to 9999-12-31 | Unlimited (64-bit signed) |
| Timezone Conversion | Converts to UTC on write | None (stores literal value) | None (pure UTC integer) |
| Year 2038 Safe | No (fails in 2038) | Yes | Yes |
How Timezone Conversion Works in MySQL
MySQL TIMESTAMP converts incoming dates from the active database session timezone into UTC on disk, then converts back to the client's session timezone on read. By contrast, DATETIME saves the exact clock numbers you give it without any timezone conversion.
If your application keeps the MySQL server session locked to UTC, DATETIME gives you a wider 1000 to 9999 year range for just 1 extra byte per row.
Converting Unix Timestamps in MySQL Queries
Use UNIX_TIMESTAMP() and FROM_UNIXTIME() to convert between MySQL dates and Unix seconds. Note an important version detail: starting in MySQL 8.0.28 on 64-bit servers, FROM_UNIXTIME() supports values up to 32,536,771,199.999999 seconds (year 3001 UTC), as stated in the MySQL 8.0 Date Functions reference. Older MySQL versions and pre-11.5 MariaDB releases return NULL past 2038 or 2106.
-- Convert a Unix timestamp into a UTC datetime
SELECT FROM_UNIXTIME(1764892800);
-- Store current Unix milliseconds in a BIGINT column
INSERT INTO event_logs (created_ms)
VALUES (FLOOR(UNIX_TIMESTAMP(NOW(3)) * 1000));
Which Column Type Should You Pick?
- Use
DATETIME(3)orDATETIME(6)when you want human-readable SQL rows, future dates past 2038, and your app always writes UTC. - Use
BIGINT UNSIGNEDwhen your API already speaks Unix milliseconds or you migrate logs straight from JavaScript and Go services. - Avoid
TIMESTAMPfor contracts, user birthdates, or long-term expiration dates because of its Year 2038 overflow limit.