4

My new project involves the requirement of manually setting a time of creation of nodes because they're looking into manually migrating parts of their old website into their new one. The date will mainly be used to sort the nodes, only 2 out of 6 content types will have the date printed in the frontend.

In D8 you can select between a date and timestamp field. Both output options seem to be the same. The only difference I noticed is the HTML output.

The timestamp output looks like this

<div class="field__item">Mon, 03/18/2019 - 18:00</div>

Date output looks like that

<time datetime="2019-03-18T18:00:17Z" class="datetime">Mon, 03/18/2019 - 18:00</time>

I found a documentation page for D7 on the matter: https://www.drupal.org/node/1455576 but I have the feeling that this information doesn't apply for those two field types in D8.

Which one should I choose for this purpose? And what's the difference in D8 between those two field types?

6
  • 1
    The difference is how they are stored in the DB. Drupal core uses timestamp. The answer to your question is... depends who you ask. See Should I use the datetime or timestamp data type in MySQL?
    – No Sssweat
    Commented Mar 19, 2019 at 7:35
  • 1
    Thank you for the link to this question. This gives a very good overview on the database side, but I was more interested in how Drupal deals with it, especially Views. There seem to be problems with both formats. since I found an issue on datetime Views treats datetime fields like strings when filtering and a SE question about timestamp Views timestamp not recognized as date? Commented Mar 19, 2019 at 7:54
  • Re the close vote, The "Which one should I choose for this purpose?" is option based, but the "And what's the difference in D8 between those two field types?" is perfectly fine here.
    – mpdonadio
    Commented Mar 19, 2019 at 12:19
  • 1
    @NoSssweat Timestamps are really just ints, and datetime are ISO strings. Neither use native datatypes in storage because they types (and functions over them) aren't portable across database backends.
    – mpdonadio
    Commented Mar 19, 2019 at 12:23
  • Aren't they stored as VARCHAR in the database? What if someone builds a field that uses native date type?
    – Kevin
    Commented Mar 19, 2019 at 13:40

2 Answers 2

6

In Drupal 8, timestamps (and created and updated) are stored as integers in the database. They do not use the native datatype. Timestamps can be added via the UI, but created and updated can only be used as basefields.

Datetime and daterange fields are stored as ISO strings: date-only as Y-m-d and date+time as the full ISO8601 format (currently without the UTC offset). These are store as plain strings, not as a native type, since the date/time handling in the default database backends is not portable.

The reason you are seeing different HTML, is that the two field types use different formatters. Your timestamp is using TimestampFormatter and it looks like your datetime is using DatetimeDefaultFormatter. It is possible to get both fields to output the same if you use the DatetimePlainFormatterformatter. You can also write your own formatter for timestamps to output HTML5 time. For various reasons, they formatters between the two types are different.

3

From a Drupal perspective, timestamp has issues with Views Filters.

In a recent D7 project, I went with timestamp just to keep things consistent since I was doing custom module date field queries.

In D7 I noticed that View filters only works if you enter a timestamp rather than a date, this is silly! Which makes me think most people use date, since it did not a have this filter issue. I got around this issue by utilizing views query alter and converting the date string to timestamp.

D8 might also have this issue. So perhaps the safest, most traveled, road is to use date.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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