3171

Would you recommend using a datetime or a timestamp field, and why (using MySQL)?

I'm working with PHP on the server side.

4
  • 2
    this has some relevant information codeproject.com/Tips/1215635/MySQL-DATETIME-vs-TIMESTAMP
    – Waqleh
    Jul 26, 2018 at 8:46
  • 11
    If you want your application to be broken in February, 2038 use timestamp. Check the date range. Mar 11, 2020 at 17:55
  • If there is even the slightest chance your database might need to store values in a different Time Zone or receive connections from an application in a different Time Zone, consider using ISO-8601 CHARs for all your important timestamps. See this question: stackoverflow.com/questions/40670532/…
    – Alex R
    Sep 19, 2020 at 18:59
  • datetime is completely useless. it's just a dumb string. timestamp is perfect in every way but it ends in 2038.
    – Fattie
    Oct 10, 2022 at 12:11

40 Answers 40

1
2
7

A TIMESTAMP requires 4 bytes, whereas a DATETIME requires 8 bytes.

1
  • 2
    "As of MySQL 5.6.4, storage for YEAR and DATE remains unchanged. However...DATETIME is packed more efficiently, requiring 5 rather than 8 bytes", so a difference of 1 byte between timestamp and datetime, with timestamp "expiring" just around the corner in 2038 :) From the docs: dev.mysql.com/doc/refman/8.0/en/… Feb 13, 2022 at 2:08
6

I like a Unix timestamp, because you can convert to numbers and just worry about the number. Plus you add/subtract and get durations, etc. Then convert the result to Date in whatever format. This code finds out how much time in minutes passed between a timestamp from a document, and the current time.

$date  = $item['pubdate']; (etc ...)
$unix_now = time();
$result = strtotime($date, $unix_now);
$unix_diff_min = (($unix_now  - $result) / 60);
$min = round($unix_diff_min);
1
  • 4
    Or, use a human readeable and native MySQL datetime, and use native MySQL functions to add / substract datetimes. Apr 25, 2013 at 10:01
6

TIMESTAMP is useful when you have visitors from different countries with different time zones. you can easily convert the TIMESTAMP to any country time zone

5

A DATETIME carries no timezone information with it and will always display the same independent of the timezone that is in effect for the session, which defaults to the server's timezone unless you have explicitly changed it. However, if I initialize a DATETIME column with a function such as NOW() rather than a literal such as '2020-01-16 12:15:00', then the value stored will, of course, be the current date and time localized to the session's timezone.

A TIMESTAMP by contrast does implicitly carry timezone information: When you initialize a TIMESTAMP column with a value, that value is converted to UTC before it is stored. If the value being stored is a literal such as '2020-01-16 12:15:00', it is interpreted as being in the session's current timezone for conversion purposes. Conversely, when a TIMESTAMP column is displayed, it will first be converted from UTC to the session's current timezone.

When to use one or the other? A Case Study

A Website for a community theater group is presenting several performances of a play for which it is selling tickets. The dates and times of these performances will appear in a drop down from which a customer wishing to buy tickets for a performance will select one. It would make sense for database column performance_date_and_time to be a DATETIME type. If the performance is in New York, there is an understanding that there is an implicit timezone involved ("New York local time") and ideally we would want the date and time to display as 'December 12, 2019 at 8:00 PM' regardless of the session's timezone and without having to go to the trouble of having to do any timezone conversions.

On the other hand, once the December 12th, 2019 8 PM performance began, we might no longer want to sell tickets for it and thus no longer display that performance in the drop down. So, we would like to be able to know whether '2019-12-12 20:00:00' has occurred or not. That would argue for having a TIMESTAMP column, setting the timezone for the session to 'America/New_York' with set session time_zone='America/New_York' and then storing '2019-12-12 20:00:00' into the TIMESTAMP column. Henceforth we can test for whether the performance has begun by comparing this column with NOW() independent of the current session timezone.

Or it might make sense to have a DATETIME and a TIMESTAMP column for these two separate purposes. Or not. Clearly, either one could serve both purposes. If you go with just a DATETIME column, then you must set the current timezone to your local timezone before comparing with NOW(). If you go with just a TIMESTAMP column, you must set the session timezone to your local timezone before displaying the column.

2
  • What matters for TIMESTAMP values but also MySQL date/time functions is not the server time_zone but the session time_zone (which defaults to the server time_zone until changed explicitely). Never rely in your app on a default that might change.
    – dolmen
    Oct 9, 2019 at 15:14
  • @dolmen Of course you are correct but we are talking semantics. I say in my answer, "... setting the the server's timezone for the session ...". Perhaps it could have been expressed more clearly by just saying "setting the timezone for the session",but in the end this will be the timezone that the server will be using. I will update the answer to make this clearer.
    – Booboo
    Oct 9, 2019 at 15:19
4

I merely use unsigned BIGINT while storing UTC ...

which then still can be adjusted to local time in PHP.

the DATETIME to be selected with FROM_UNIXTIME( integer_timestamp_column ).

one obviously should set an index on that column, else there would be no advance.

3

A lot of answers here suggest to store as timestamp in the case you have to represent well defined points in time. But you can also have points in time with datetime if you store them all in UTC by convention.

4
  • Can you give some reasons why I should prefer this instead of using timestamp?
    – Jasir
    Aug 12, 2016 at 8:02
  • 3
    @Jasir not really, it seems a big debate. I was only pointing out that datetime can represent specific points in time if there is a convention to store all datetimes in a specific timezone (i.e. UTC). Personally, I would store everything in datetime, timestamp being limited to 2038 (even though I guess they will eventually do something about it), and if I look at some raw data in DB, datetime will be readable without having to apply conversion. Again, this is very subjective.
    – Matthew
    Aug 12, 2016 at 11:41
  • @Matthew But if the session time_zone is not UTC too, you will have surprises (bugs) if you use MySQL date/time functions such as NOW(), DATE_ADD without converting timezone first... So why make database interaction more complicated than necessary?
    – dolmen
    Oct 9, 2019 at 15:06
  • 1
    @dolmen the idea is that server-side everything is UTC. MySQL session time zone included. This should be set as the server's time zone which should be UTC
    – Matthew
    Oct 10, 2019 at 1:54
3

Not mentioned so far, is that DEFAULT CURRENT_TIMESTAMP only works with Timestamp, but not DateTime type fields.

This becomes relevant for MS Access tables which can only use DateTime but not Timestamp.

2
  • 9
    The DEFAULT CURRENT_TIMESTAMP support for a DATETIME is added in MySQL 5.6. Mar 27, 2017 at 7:59
  • 1
    "Any of the synonyms for CURRENT_TIMESTAMP have the same meaning as CURRENT_TIMESTAMP. These are CURRENT_TIMESTAMP(), NOW(), LOCALTIME, LOCALTIME(), LOCALTIMESTAMP, and LOCALTIMESTAMP()." -> mysql docs
    – CPHPython
    Apr 26, 2018 at 15:20
1

If you want to GUARANTEE your application will NOT function in February, 2038, use TIMESTAMP. Refer to your REFMAN for the RANGE of dates supported.

1
  • 2038 is so far that you will have changed the MySQL server version to one that supports extended TIMESTAMP long before. And this also assumes your application will still be in use.
    – dolmen
    Oct 9, 2019 at 15:09
1

The DATETIME type is used for values containing date and time parts. MySQL retrieves and displays DATETIMEvalues in format. The supported range is . 'YYYYYY-MM-DD hh:mm:ss' '1000-01-01 00:00:00''9999-12-31 23:59:59'

The TIMESTAMP data type is used for values containing date and time parts. TIMESTAMP has a range from '1970-01-01 00:00:01'UTC to '2038-01-19 03:14:07'UTC.

mysql> SELECT col,
     >     CAST(col AT TIME ZONE INTERVAL '+00:00' AS DATETIME) AS ut
     >     FROM ts ORDER BY id;
+---------------------+---------------------+
| col                 | ut                  |
+---------------------+---------------------+
| 2020-01-01 10:10:10 | 2020-01-01 15:10:10 |
| 2019-12-31 23:40:10 | 2020-01-01 04:40:10 |
| 2020-01-01 13:10:10 | 2020-01-01 18:10:10 |
| 2020-01-01 10:10:10 | 2020-01-01 15:10:10 |
| 2020-01-01 04:40:10 | 2020-01-01 09:40:10 |
| 2020-01-01 18:10:10 | 2020-01-01 23:10:10 |
+---------------------+---------------------+

URL MySQL 8.0 : https://dev.mysql.com/doc/refman/8.0/en/datetime.html

0

timestamp is a current time of an event recorded by a computer through Network Time Protocol (NTP).

datetime is a current timezone that is set in your PHP configuration.

2
  • No. Unrelated to NTP.
    – dolmen
    Oct 9, 2019 at 15:16
  • And in 2039, use of timestamp will ensure your application will not be functioning. Mar 1 at 17:57
1
2

Not the answer you're looking for? Browse other questions tagged or ask your own question.