Problem/Motivation

TimeAgo's granularity system cuts off intervals in ways that lead to misleading outputs, and the formatting options are very inflexible. For example, if I set granularity 4 and ask for TimeAgo from 2020/1/10 to 2020/2/12 (day of post), the output will be simply "1 month". 2020/1/5 to 2020/2/12 will give me "1 month 1 week", and 2020/1/4 to 2020/2/12 will give me "1 month 1 week 1 day 1 hour".

The reason for this is that the formatter looks for the first non-zero value in the PHP DateInterval object (which contains years, months, days, hours, minutes, seconds) and starts counting its granularity from there, but then cuts off at the next 0 value it runs into. In the above example, weeks is not part of the PHP DateInterval object, so it's computed in Drupal\Core\Datetime\DateFormatter's formatDiff function as floor(days/7). If that returns 0, it won't display "weeks" or anything that would come after it. So for 1 month 1 day to 1 month 6 days, the only output you will get is "1 month", which is misleading at best and inaccurate if taken at face value.

The format can't be worked around either. The output of TimeAgo is not customizable beyond the (theoretical maximum) granularity value. For example, for my purposes I need months and days but I don't want weeks, and there is no way to request output in this form.

Proposed resolution

The native PHP DateInterval object is already a fairly flexible, accurate, and customizable thing. Rather than a simple granularity level and being able to put text before or after the @interval object, I would love a full set of formatting options and placeholders like we get for other DateTime objects would both guarantee accurate output and allow users to customize the output to their needs. So, for example, a user could specify "@y" for total years, "@m" for total months, and ideally more complex options like, e.g., "@ym" for "months within year" (e.g., Jan 2019 to Feb 2020 is 13 months, so @m would give you 13 while @y would give you 1, but "@y Years @ym Months" could output "1 year 1 month") "@md" for "days within month", etc.

Remaining tasks

A complete rework of the formatDiff function in DateFormatter, and related config options.

User interface changes

Changed options for TimeAgo display format, now allowing more granular placeholders.

API changes

TBD

Data model changes

Anything that references the TimeAgo format.

Comments

jkominsky created an issue. See original summary.

brewerkr’s picture

+1

Version: 8.9.x-dev » 9.1.x-dev

Drupal 8.9.0-beta1 was released on March 20, 2020. 8.9.x is the final, long-term support (LTS) minor release of Drupal 8, which means new developments and disruptive changes should now be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 11.x-dev » main

Drupal core is now using the main branch as the primary development branch. New developments and disruptive changes should now be targeted to the main branch.

Read more in the announcement.