We have moved to Github. Please open tickets there.

Opened 3 years ago

Closed 3 years ago

#1531 closed Defect (fixed)

Friendlier Time

Reported by: jricher Owned by:
Priority: critical Milestone: Elgg 1.7
Component: Core Version: 1.7
Severity: minor Keywords:
Cc: brettp Difficulty:

Description

Formats friendly times older than 2 weeks as a simple date, using the language file and elgg_echo to determine the date format (allows for custom localization of dates, too).

Attachments (1)

friendlier_time.patch (2.0 KB) - added by jricher 3 years ago.
expands the friendly time function

Download all attachments as: .zip

Change History (9)

Changed 3 years ago by jricher

expands the friendly time function

comment:1 Changed 3 years ago by cash

When dealing with this, also see #1265 which I'm closing as a duplicate (even though it's not exactly). Also see #451

comment:2 Changed 3 years ago by jricher

I did it this way as a compromise between the two alternatives posited in #1265 that uses both where reasonable (at least, as I thought).

Ultimately, I'd rather have there be a programmatic way to set the time and date formatting for different time periods, and to set the time periods themselves, from an admin console. One of our sites here had actually overridden the friendly_time function call to *always* post the date string (in a hardcoded format), and it'd be nice to have that as a configuration option. But until we figure out how to do that, I think this is a good stopgap that'd keep us from having another local patch against 1.7 final.

As to #451, I'd be happy to see this use strftime instead of date -- but where do we get the info to send into setlocale in the first place? Without that, strftime is as limited as date, it seems.

comment:3 Changed 3 years ago by cash

Decided not to close #1265 for the time being.

Eventually Elgg is going to have to deal with locale and timezones (especially if we allow absolute timestamps). I'm assuming we'd do it by allowing the admin to select the defaults and users can set their own location just like they can set their own language.

What we did was use the acronym tag on timestamps so it shows the friendly time and if you mouse over, it shows absolute.

comment:4 Changed 3 years ago by brettp

(In [svn:3957]) Refs #1531: Added full time and dates to friendly time stamptes via acronym tags.

comment:5 Changed 3 years ago by brettp

  • Resolution set to Closed
  • Status changed from new to closed

I've used Cash's approach for 1.7 because it provides added functionality without UI changes. Some people might prefer the "friendly times" so until we can provide an admin option, let's keep it as it has been.

Closing this ticket in favor of #1265 and #451.

comment:6 Changed 3 years ago by thomas

  • Priority changed from normal to critical
  • Resolution Closed deleted
  • Status changed from closed to reopened

Sibce [svn:3957] the logic is broken, because the old "return" skiped the rest of the function. Now

$friendly_time = sprintf(elgg_echo("friendlytime:hours:singular"), $diff);

will be always the winner, because it stands outside an else {} and will always overide

if ($diff > 1) {
			$friendly_time = sprintf(elgg_echo("friendlytime:hours"), $diff);
}

Here the complete fix

function friendly_time($time) {
	$diff = time() - ((int) $time);

	if ($diff < 60) {
		return elgg_echo("friendlytime:justnow");
	} else if ($diff < 3600) {
		$diff = round($diff / 60);
		if ($diff == 0) {
			$diff = 1;
		} 
		if ($diff > 1) {
			$friendly_time = sprintf(elgg_echo("friendlytime:minutes"), $diff);
		} else {
			$friendly_time = sprintf(elgg_echo("friendlytime:minutes:singular"), $diff);
		}
	} else if ($diff < 86400) {
		$diff = round($diff / 3600);
		if ($diff == 0) {
			$diff = 1;
		}
		if ($diff > 1) {
			$friendly_time = sprintf(elgg_echo("friendlytime:hours"), $diff);
		} else {
			$friendly_time = sprintf(elgg_echo("friendlytime:hours:singular"), $diff);
		}
	} else {
		$diff = round($diff / 86400);
		if ($diff == 0) {
			$diff = 1;
		}
		if ($diff > 1) {
			$friendly_time = sprintf(elgg_echo("friendlytime:days"), $diff);
		} else {
			$friendly_time = sprintf(elgg_echo("friendlytime:days:singular"), $diff);
		}
	}

	$timestamp = htmlentities(date(elgg_echo('friendlytime:date_format', $time)));
	return "<acronym title=\"$timestamp\">$friendly_time</acronym>";
}

comment:7 Changed 3 years ago by thomas

And another one:

$timestamp = htmlentities(date(elgg_echo('friendlytime:date_format'), $time));

There was missing a ) after elgg_echo('friendlytime:date_format', so always the actual time was returned

comment:8 Changed 3 years ago by brettp

  • Resolution set to fixed
  • Status changed from reopened to closed

(In [svn:3965]) Fixes #1531: Correctly implemented dates for friendly times. No more coding while watching the Olympics!

Note: See TracTickets for help on using tickets.