Updated: Comment #23

Problem/Motivation

The Facebook iphone app shows "just now" after posting new content. I thought that's kind of cool and should be implemented in Drupal.

Proposed resolution

Maybe show "just now" after posting nodes and comments, if it's less than 30 seconds ago.

Remaining tasks

-Patches are to be reviewed.

User interface changes

None

API changes

None.

Original report by mototribe

Issue fork drupal-1403508

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

Everett Zufelt’s picture

Version: 7.x-dev » 8.x-dev
Component: node system » node.module
Issue tags: +Usability

Feature requests are made against Drupal 8.x

dman’s picture

Issue tags: +Novice

Sounds easy. Tagging as novice as a good exercise in patching.

The question stands whether everyone WANTs to do this - and there may be some discussion needed.
However, the discussion can start on a good basis if someone can provide a patch that actually does the change.

dman’s picture

Issue tags: +DDU2012

Tagging for code sprint DDU2012

gro’s picture

It does sound easy (and cool!) but turns out not to be a simple string replacement.

The different modules that add the time in this fashion (ie comment) use '@time' (defined by format_interval) and append 'ago' so to change this would require a new function similar to format_interval that could define 'human readable' terms. That could possibly be inappropriate for a core feature request because it's not a functionality improvement and more appropriate as a module.

...but I'm a total novice

dman’s picture

Yeah, we looked at this together and came to the conclusion that to do it "right" would require an additional util function in the family of format_date(), format_duration() - we could also have format_age() in order to provide full, nice support for ["just now", "yesterday", "1 hour ago", etc].

While that would be cool, and a cute project - right now it would also mean touching a few too many places that currently use t('@time ago', ...) as their string constructs.

If folk would like to say "YES, it would be nice to support a set of configurable, translatable, human text strings to represent age" then this work would be nice to do.
But I'd rather test the water and sniff out if there is any chance it would be accepted before suggesting this functionality be developed further.

It can't practically be added as a contrib module (that I can see) ... as I can't see that we could leverage or intercept the t() call enough to rewrite it like that.

jtbayly’s picture

Issue tags: -Novice

As a novice, looking for a place to try out submitting new patches, I'm going to go out on a limb and remove the "novice" tag from this issue. :)

-Joseph

scorchio’s picture

Status: Active » Needs review
StatusFileSize
new8.5 KB

Let's see how this patch looks. The result is kinda rough, but it's working reasonably well on the recent comments block for example.

Based on dman's suggestions in #5 I've created format_age() and modified the code at several places where "@time ago" was used with format_interval().

tstoeckler’s picture

Nice, that looks quite good already.

A couple notes:
1. $timestamp is not actually a timestamp. Let's call it $interval or $seconds or something. Actually looking at the patch, it is being called with REQUEST_TIME - $timestamp every single time. So perhaps we should inline that in this function. I.e.:

// No more subtracting from REQUEST_TIME.
format_age($node->created);

function format_age($timestamp, $langcode = NULL) {
  // We do that for you. You're welcome.
  $interval = REQUEST_TIME - $timestamp;
  ...
}

In that case $timestamp would be fine.


2. Instead of calling format_plural() with the singular/plural strings manually, I think it's better to call format_interval() for that. That would allow to simplify the function a bit. Something like:

  // See above for why I call this $interval.
  if ($interval < 30) {
    $output = t('Just now');
  }
  else {
    $output = format_interval($interval);
  }



3. It was suggested above to support things like "Last year" instead of "1 year ago" as well. Don't know how that would fit in, but wanted to mention that.

dman’s picture

Moving ahead.
Some notes, I think that the t() in

t('@age', array('@age' => format_age(REQUEST_TIME - $comment->changed)))

in a few places is redundant.

And I agree that it would be helpful to do the $interval = REQUEST_TIME - $timestamp; for them as tstoeckler suggests. Less redundancy.
That leaves us with just

  format_age($comment->changed);

I'm not sure how translation will deal with

+    '1 hour ago|@count hours ago' => 3600,
+    '1 minute ago|@count minutes ago' => 60,
+    '1 second ago|@count seconds ago' => 30

sort of things (I'll assume it all works as required), but it's taking shape.

Ideally, I imagined that there would be a admin-defined sort of granularity so we can choose between "12 hours ago" and "today" depending on preferences. But I don't really know the best algorithm for that sort of flexibility. Or imagine the UI for it. I'm sure there is some prior art on that.

scorchio’s picture

StatusFileSize
new8.47 KB

tstoeckler: sure, $timestamp should be $interval. I've copied that part from format_interval() for starting the patch, so actually this is two minor issue in one :) See #1416218: Improve variable naming in format_interval().

About calling format_interval(): if we would like to display text like "last year" (as my current patch does), we shouldn't call format_interval() - both the English and different localized texts will differ in the two functions.

This patch implements suggestions from #8 and #9. Do you have any ideas on how to improve it further?

Status: Needs review » Needs work

The last submitted patch, show_just_now-1403508-10.patch, failed testing.

tstoeckler’s picture

+++ b/core/includes/common.inc
@@ -1844,6 +1844,44 @@ function format_interval($timestamp, $granularity = 2, $langcode = NULL) {
+  $units = array(
...
+  );
...
+  foreach ($units as $key => $value) {

I think it makes sense to initizialize the array reversely, so that you don't have to explode the $key. I.e.:

array(
  3156000 => array('last year', '@count years ago'),
  ...
);
+++ b/core/includes/common.inc
@@ -1844,6 +1844,44 @@ function format_interval($timestamp, $granularity = 2, $langcode = NULL) {
+    'a day ago|@count days ago' => 86400,

That's what "yesterday" means. :) (Not trying to make fun of you (!), I just had to smile reading that.)

+++ b/core/includes/common.inc
@@ -1844,6 +1844,44 @@ function format_interval($timestamp, $granularity = 2, $langcode = NULL) {
+    'a second ago|@count seconds ago' => 30

If I read the code below correctly, "a second ago" weill never be called, right?

+++ b/core/includes/common.inc
@@ -1844,6 +1844,44 @@ function format_interval($timestamp, $granularity = 2, $langcode = NULL) {
+      if ($value === 30) { $value = 1; }

We don't do the short syntax for if clauses, that should be split into multiple lines. More importantly, though, I don't really like this. :) I think we could merge the part below (the 'just now' part) and cut this part. That would make the whole thing easier to read.

scorchio’s picture

+++ b/core/includes/common.inc
@@ -1844,6 +1844,44 @@ function format_interval($timestamp, $granularity = 2, $langcode = NULL) {
+    'a day ago|@count days ago' => 86400,

That's what "yesterday" means. :) (Not trying to make fun of you (!), I just had to smile reading that.)

Please correct me if I'm wrong, but as I actually use "yesterday" in daily life, it means the last day in the calendar: midnight to midnight. If we modify "a day ago" to "yesterday" it could lead to some confusion, let me show you an example:

If it's actually 6AM...

  • now - 24 hours = yesterday 6AM - the current patch says "a day ago"; you suggest "yesterday", which would be OK.
  • now - 31 hours = day before yesterday 11PM - the current patch says "a day ago"; you suggest "yesterday", which is not correct in my interpretation.
dman’s picture

True enough. If we are aiming for fully natural language (which I feel is starting to get tricky) then at 1AM a post made 2 hours ago is "yesterday".

My local TV station confuses me when it does this in reverse and an ad comes on at 1AM for shows that will be on "tonight" - meaning the following evening. Anyway.

Yes, I think what scorchio is saying is right. (a day ago/yesterday) isn't just the span of time between now-24 and now-48 .

I knew we'd get into trouble :-)

I tried a really quick search and found things like
http://stackoverflow.com/questions/11/how-do-i-calculate-relative-time
http://www.php.net/manual/en/function.strtotime.php#100773
http://www.ferdychristant.com/blog//archive/DOMM-7QEFK4
And see more quibbles than agreements on the 'right way'. Hm :-(

It seems there is even a jquery solution for it http://timeago.yarp.com/
.. which conveniently manages future times also!!
Could be worth comparing with that algorithm there
https://github.com/rmm5t/jquery-timeago/blob/master/jquery.timeago.js

mototribe’s picture

wow, I had no idea that my little suggestion created that much work ;-)
Personally, I like the Stackoverflow solution.

scorchio’s picture

I will check out that timeago plugin and play around with it for a little - it looks nice.

tstoeckler’s picture

Oh yes, you are of course correct. Stupid me!
To calculate whether it is "yesterday" or not, we could use something like

// 'w' returns 0 for Sunday, 1 for Monday, etc.
$day_now = format_date(REQUEST_TIME, 'custom', 'w');
// Because $day_now - 1 can be negative, we add 7 and do the modulus. That way -1 turns to 6 and the rest stays the same.
$day_yesterday = (($day_now - 1) + 7) % 7;
// $timestamp is the actual timestamp not $interval here.
$day_before = (format_date($timestamp, 'custom', 'w');
if ($day_yesterday == $day_before) {
 ...
}

// Of course that could be written shorter:
if (format_date($timestamp, 'custom', 'w') == ((format_date(REQUEST_TIME, 'custom', 'w') + 6) % 7)) {
 ...
}

That's just one way, though, if anything else is easier/better/etc. that's fine. It just seemed more intuitive to me, because that way we can hand down the calculation of the day down to PHP.

scorchio’s picture

Status: Needs work » Needs review
StatusFileSize
new9.52 KB

I wonder how much we would like to "humanize" our date intervals. Implementing "yesterday" is one thing, but then we should have "last month" instead of "a month ago", "last year" instead of "a year ago" etc. That would mean we should find similar "workarounds" for each one, like in #17.

I don't know which way would be the better, what do you think?

I've checked Timeago and it looks nice, so here is a patch which implements Timeago's algorithm modified a little bit (so we have "just now").

Also, format_age() in the patch has $end, an optional timestamp argument which defaults to REQUEST_TIME. What do you think about this? Is there situations when setting a different "reference point" could be useful?

The thing I really like in Timeago is the idea of implementing fuzzy time calculation on the client's side - every age stays up-to-date all the time. What do you think about having a similar solution in Drupal?

p.s. I'm trying to follow the coding standards as much as possible, so any further advice on that is very welcome.

The last submitted patch, show_just_now-1403508-18.patch, failed testing.

scorchio’s picture

Status: Needs work » Needs review

#18: show_just_now-1403508-18.patch queued for re-testing.

wryz’s picture

#18: show_just_now-1403508-18.patch queued for re-testing.

Status: Needs review » Needs work

The last submitted patch, show_just_now-1403508-18.patch, failed testing.

PavanL’s picture

Issue summary: View changes
xjm’s picture

Component: node.module » node system

(Merging "node system" and "node.module" components for 8.x; disregard.)

Robin_K’s picture

Issue tags: +Amsterdam2014

I quite like this idea, I'm going to try to convert this patch into something usable for the current dev.

Version: 8.0.x-dev » 8.1.x-dev

Drupal 8.0.6 was released on April 6 and is the final bugfix release for the Drupal 8.0.x series. Drupal 8.0.x will not receive any further development aside from security fixes. Drupal 8.1.0-rc1 is now available and sites should prepare to update to 8.1.0.

Bug reports should be targeted against the 8.1.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.2.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.1.x-dev » 8.2.x-dev

Drupal 8.1.9 was released on September 7 and is the final bugfix release for the Drupal 8.1.x series. Drupal 8.1.x will not receive any further development aside from security fixes. Drupal 8.2.0-rc1 is now available and sites should prepare to upgrade to 8.2.0.

Bug reports should be targeted against the 8.2.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.3.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

yoroy’s picture

Issue tags: +sprint

Still like this idea :)

Version: 8.2.x-dev » 8.3.x-dev

Drupal 8.2.6 was released on February 1, 2017 and is the final full bugfix release for the Drupal 8.2.x series. Drupal 8.2.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.3.0 on April 5, 2017. (Drupal 8.3.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.3.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.4.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.3.x-dev » 8.4.x-dev

Drupal 8.3.6 was released on August 2, 2017 and is the final full bugfix release for the Drupal 8.3.x series. Drupal 8.3.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.4.0 on October 4, 2017. (Drupal 8.4.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.4.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.4 was released on January 3, 2018 and is the final full bugfix release for the Drupal 8.4.x series. Drupal 8.4.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.5.0 on March 7, 2018. (Drupal 8.5.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.5.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.6 was released on August 1, 2018 and is the final bugfix release for the Drupal 8.5.x series. Drupal 8.5.x will not receive any further development aside from security fixes. Sites should prepare to update to 8.6.0 on September 5, 2018. (Drupal 8.6.0-rc1 is available for testing.)

Bug reports should be targeted against the 8.6.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.6.x-dev » 8.8.x-dev

Drupal 8.6.x will not receive any further development aside from security fixes. Bug reports should be targeted against the 8.8.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.9.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: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.7 was released on June 3, 2020 and is the final full bugfix release for the Drupal 8.8.x series. Drupal 8.8.x will not receive any further development aside from security fixes. Sites should prepare to update to Drupal 8.9.0 or Drupal 9.0.0 for ongoing support.

Bug reports should be targeted against the 8.9.x-dev branch from now on, and new development or disruptive changes should 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: 8.9.x-dev » 9.2.x-dev

Drupal 8 is end-of-life as of November 17, 2021. There will not be further changes made to Drupal 8. Bugfixes are now made to the 9.3.x and higher branches only. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

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

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

Drupal 9.3.15 was released on June 1st, 2022 and is the final full bugfix release for the Drupal 9.3.x series. Drupal 9.3.x will not receive any further development aside from security fixes. Drupal 9 bug reports should be targeted for the 9.4.x-dev branch from now on, and new development or disruptive changes should 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.4.x-dev » 9.5.x-dev

Drupal 9.4.9 was released on December 7, 2022 and is the final full bugfix release for the Drupal 9.4.x series. Drupal 9.4.x will not receive any further development aside from security fixes. Drupal 9 bug reports should be targeted for the 9.5.x-dev branch from now on, and new development or disruptive changes should 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.

Aditi Saraf made their first commit to this issue’s fork.

aditi saraf’s picture

StatusFileSize
new3.27 KB

According to me Fr time interval <= 1 second we should show just now . For rest Drupal it self is handling .

aditi saraf’s picture

Status: Needs work » Needs review
smustgrave’s picture

Status: Needs review » Needs work

Seems to contain a lot of out of scope changes.

If a different solution is going to be used the issue summary needs to be updated.

Also no test coverage.

Version: 9.5.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. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

acbramley’s picture

Component: node system » comment.module
Issue tags: -sprint +Needs issue summary update, +Needs reroll

The current patch is just changing the output for comments in Olivero, it doesn't seem like there would be a way to do this generically for all themes.

I also can't see any similar strings related to node so moving to comment.module

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.