Which format to use to store dates in database with MySQL / MariaDB?
This question may seem odd at first, but depending on the use case, the answer is not as obvious as it looks. I talk mostly about MySQL and MariaDB here, but the principles apply to any database. For simplicity, I only cover the case of a date with a time; a date alone is just a simpler version of it.
Tested with MySQL 8.4 LTS, MySQL 9.7 LTS and MariaDB 11.8 LTS (November 2026). MySQL and MariaDB differ on several points, which are flagged below.
TL;DR: DATETIME is easy to use but does not store the time zone. TIMESTAMP is converted with the session time_zone and stops in 2038 on MySQL (2106 on MariaDB 11.5+).
The most robust option is a Unix timestamp in a BIGINT column, handled by the application, plus a time zone column when the date depends on a place (concerts, events).
Whatever you choose, use the same format across the whole database.
DATETIME, TIMESTAMP, BIGINT or VARCHAR: comparison
DATETIME | TIMESTAMP | BIGINT (Unix timestamp) | VARCHAR (ISO 8601) | |
|---|---|---|---|---|
| Range | 1000-01-01 to 9999-12-31 | 1970-01-01 to 2038-01-19 (MySQL), 2106-02-07 (MariaDB 11.5+, 64 bits) | Unlimited in practice | Unlimited |
| Storage | 5 bytes | 4 bytes | 8 bytes | Length + 1 byte (about 26 for 2004-02-12T15:19:21+00:00) |
| Fractional seconds | Yes, DATETIME(6), + 0 to 3 bytes | Yes, TIMESTAMP(6), + 0 to 3 bytes | No (or store milliseconds) | Yes |
| Time zone | Not stored | Converted to and from UTC with the session time_zone, not stored | Always UTC, by definition | Stored (offset) |
| Automatic value | DEFAULT CURRENT_TIMESTAMP, ON UPDATE | DEFAULT CURRENT_TIMESTAMP, ON UPDATE | DEFAULT (UNIX_TIMESTAMP()), no ON UPDATE | No |
| SQL date functions | Directly | Directly | After FROM_UNIXTIME() | After conversion |
| Sorting and range queries | Yes | Yes | Yes | Wrong when offsets differ |
Sizes from the MySQL storage requirements; ranges from the MySQL and MariaDB documentation.
The obvious answer: DATETIME
Until a few years ago, I used DATETIME without thinking about it. It is easy to use, its format is close to PHP's, and SQL offers many functions to manipulate it (see my articles on dates in SQL and dates in PHP).
But a DATETIME is only a date and a time, without context: when you read it back, there is no way to know in which time zone it was written. And even if you know the time zone (from another column, for example), displaying it in another time zone is up to you.
Since MySQL 8.0.19, you can write a date with an offset, but the offset is not stored: MySQL converts the value to the session time zone and forgets it. MariaDB rejects this syntax.
CREATE TABLE test_dt (d DATETIME NOT NULL); SET time_zone = '+00:00'; INSERT INTO test_dt (d) VALUES ('2026-11-07 10:00:00+04:00'); SELECT d FROM test_dt; -- MySQL 8.4 and 9.7 +---------------------+ | d | +---------------------+ | 2026-11-07 06:00:00 | +---------------------+ -- MariaDB 11.8 ERROR 1292 (22007): Incorrect datetime value: '2026-11-07 10:00:00+04:00'
Another difference to know if you migrate from one to the other: with fractional seconds beyond the column precision, MySQL rounds and MariaDB truncates. '2026-11-07 10:00:00.987654' in a DATETIME column gives 10:00:01 on MySQL and 10:00:00 on MariaDB.
The false friend: TIMESTAMP
You might think the TIMESTAMP type is the solution, but its name is misleading. It does not accept a Unix timestamp and does not return one:
CREATE TABLE test_ts ( id INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY, dt DATETIME NOT NULL, ts TIMESTAMP NOT NULL ); INSERT INTO test_ts (dt, ts) VALUES (1515359139, 1515359139); ERROR 1292 (22007): Incorrect datetime value: '1515359139' for column 'dt' at row 1
On older versions without strict mode, this query was accepted with a warning and stored 0000-00-00 00:00:00. Today it fails on both engines. In fact, TIMESTAMP behaves like DATETIME; the difference is internal: MySQL converts the date to UTC for storage, then back to the session time zone (time_zone variable) when reading. If the server or the connection configuration changes between writing and reading, the displayed value changes:
SET time_zone = '+00:00'; INSERT INTO test_ts (dt, ts) VALUES ('2018-01-07 22:10:00', '2018-01-07 22:10:00'); SET time_zone = '+04:00'; SELECT * FROM test_ts; +----+---------------------+---------------------+ | id | dt | ts | +----+---------------------+---------------------+ | 1 | 2018-01-07 22:10:00 | 2018-01-08 02:10:00 | +----+---------------------+---------------------+
Like DATETIME, it does not keep the time zone of the writer. And on MySQL, it is stored on 32 bits: no date after 2038-01-19 03:14:07 UTC. In strict mode, enabled by default, the insert fails. MariaDB 11.5 extended the range to 2106 on 64-bit platforms:
SET time_zone = '+00:00'; INSERT INTO test_ts (dt, ts) VALUES ('2040-01-01 10:10:00', '2040-01-01 10:10:00'); -- MySQL 8.4 and 9.7 ERROR 1292 (22007): Incorrect datetime value: '2040-01-01 10:10:00' for column 'ts' at row 1 -- MariaDB 11.8: accepted, the limit is now 2106-02-07 06:28:15 UTC
Alternative: a Unix timestamp in an integer
One solution is not to ask the database to be smart. Its job is to store data reliably, not to guess the time zone of your users. The idea is to store a Unix timestamp (seconds since 1970-01-01 UTC) in an integer column and let the application handle it.
Use a BIGINT: a signed INT stops at 2038-01-19 03:14:07 UTC, an INT UNSIGNED at 2106. To read the value during an SQL query, use FROM_UNIXTIME(). Since MySQL 8.0.13 (and MariaDB 10.2), an expression default even fills the column automatically:
SET time_zone = '+00:00'; CREATE TABLE test_int ( id INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY, created_at BIGINT UNSIGNED NOT NULL DEFAULT (UNIX_TIMESTAMP()) ); INSERT INTO test_int (created_at) VALUES (1515359139), (4102444800), (253402300799); SELECT id, created_at, FROM_UNIXTIME(created_at) FROM test_int; +----+--------------+---------------------------+ | id | created_at | FROM_UNIXTIME(created_at) | +----+--------------+---------------------------+ | 1 | 1515359139 | 2018-01-07 21:05:39 | | 2 | 4102444800 | 2100-01-01 00:00:00 | | 3 | 253402300799 | NULL | +----+--------------+---------------------------+
FROM_UNIXTIME() goes up to 3001-01-18 on MySQL and 2106-02-07 on MariaDB, then returns NULL. That is more than enough for most applications, and PHP handles the rest:
$ php -r "echo (new DateTime('@253402300799'))->format('Y-m-d H:i:s');" 9999-12-31 23:59:59
Advantages
The application handles the display format, and the database stays simple: its role is to store data, not to carry business logic.
A simple example of a DATETIME display problem is the comments on this blog. Someone on Reunion Island (UTC+4) posts a message on January 27 at 1 am: 2018-01-27 01:00:00 is recorded. In France, it is only 10 pm on January 26. If I reply quickly, my answer is recorded at 2018-01-26 22:30:00, and is displayed before the question! If my site only uses the French time zone, the order is right, but my reader sees their message dated the day before, which is not great either.
If all dates are stored as timestamps, the application displays them according to the location of each user, and everybody is happy.
Disadvantages
The value is not readable without FROM_UNIXTIME(), and ON UPDATE CURRENT_TIMESTAMP does not exist for integers: an "updated at" column must be filled by the application or a trigger.
Moreover, depending on the data, the display of a date sometimes does not depend on the user but on the data itself. Take a site that lists shows, concerts or festivals: the schedule only makes sense in the time zone of the venue. The time zone is then fixed for each event, and the simplest way is to store the timestamp and the time zone in two columns:
CREATE TABLE event ( id INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY, name VARCHAR(100) NOT NULL, place VARCHAR(100) NOT NULL, date_ts BIGINT UNSIGNED NOT NULL, tz VARCHAR(50) NOT NULL ); INSERT INTO event (name, place, date_ts, tz) VALUES ('Metallica', 'Accor Hotel Arena, Paris', 1504800000, 'Europe/Paris'); SELECT * FROM event; +----+-----------+--------------------------+------------+--------------+ | id | name | place | date_ts | tz | +----+-----------+--------------------------+------------+--------------+ | 1 | Metallica | Accor Hotel Arena, Paris | 1504800000 | Europe/Paris | +----+-----------+--------------------------+------------+--------------+
And when reading the data in PHP:
<?php $conn = new PDO('mysql:dbname=test;host=127.0.0.1', 'root', null); $sql = 'SELECT name, place, date_ts, tz FROM event LIMIT 1'; foreach ($conn->query($sql) as $row) { $date = new DateTime('@'.$row['date_ts']); // with a timestamp, the time zone parameter is ignored: the date is in UTC $date->setTimezone(new DateTimeZone($row['tz'])); echo $date->format(DATE_ATOM); } // 2017-09-07T18:00:00+02:00
A variant is to store the date in UTC in a DATETIME column, written with UTC_TIMESTAMP() or by the application. It stays readable and usable with all SQL functions, provided that every writer respects the convention.
Other ideas: strings
Timestamp in a VARCHAR
Storing the timestamp in a VARCHAR would remove the size limit of the integer. But a BIGINT already goes far beyond year 9999. The VARCHAR also takes more space: a BIGINT uses 8 bytes, a VARCHAR uses one byte per character plus one (two beyond 255 bytes), so 11 bytes for a current 10-digit timestamp. Worse, sorting compares strings: '999999999' (2001-09-09 01:46:39 UTC) comes after '1000000000', one second later. This solution has no advantage over the integer.
Full ISO 8601 date in a VARCHAR
To keep the time zone, you could store the date in ISO 8601 format (1997-07-16T19:20:30.45+01:00) in a VARCHAR. But sorting and comparisons are done on the string, not on the instant: as soon as the offsets differ, the order is wrong.
CREATE TABLE test_iso ( id INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY, date_iso VARCHAR(35) NOT NULL ); INSERT INTO test_iso (date_iso) VALUES ('1997-07-16T19:20:30.45+01:00'), ('2004-02-12T15:19:21+00:00'), ('2004-02-12T15:19:00-05:00'), ('2004-02-12T15:19:21+01:00'); SELECT * FROM test_iso ORDER BY date_iso; +----+------------------------------+ | id | date_iso | +----+------------------------------+ | 1 | 1997-07-16T19:20:30.45+01:00 | | 3 | 2004-02-12T15:19:00-05:00 | | 2 | 2004-02-12T15:19:21+00:00 | | 4 | 2004-02-12T15:19:21+01:00 | +----+------------------------------+
In UTC, row 3 is 20:19, row 2 is 15:19 and row 4 is 14:19: the chronological order is 1, 4, 2, 3. This format is only usable if all dates have the same offset, and then a DATETIME in UTC does the job better.
Which one to choose?
| Use case | Recommended format |
|---|---|
| Technical dates (creation, update) on a single-time-zone site | DATETIME with DEFAULT CURRENT_TIMESTAMP |
| Users in several time zones (comments, messages, logs) | BIGINT Unix timestamp, or DATETIME always in UTC |
| Date tied to a place (event, appointment, flight) | Timestamp or UTC DATETIME + time zone column (Europe/Paris) |
| Dates after 2038 | Anything but TIMESTAMP on MySQL |
| Calendar date without time (birthday) | DATE |
FAQ
What is the difference between DATETIME and TIMESTAMP in MySQL?
Both display YYYY-MM-DD hh:mm:ss. TIMESTAMP is converted to UTC on write and back to the session time zone on read, uses 4 bytes and stops in 2038 on MySQL. DATETIME stores the value as is, uses 5 bytes and goes up to year 9999.
Is MySQL affected by the year 2038 problem?
Yes for TIMESTAMP columns, still limited to 2038-01-19 03:14:07 UTC in MySQL 8.4 and 9.7. DATETIME and BIGINT are not affected. MariaDB 11.5+ pushed the limit to 2106 on 64-bit platforms.
How do I store a date in UTC?
Either a BIGINT filled with UNIX_TIMESTAMP(), or a DATETIME filled with UTC_TIMESTAMP(). In both cases, convert to the user's time zone at display time.
Conclusion
There is no perfect solution: it depends on your use cases. But if I can give only one piece of advice, it is to choose a format and stick to it across the whole database. There is nothing worse to maintain than a database that mixes formats for this kind of column, or for its naming (and I know what I'm talking about).
Add a comment