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
Comment #2
brewerkr commented+1