Take the 2-minute tour ×
Drupal Answers is a question and answer site for Drupal developers and administrators. It's 100% free, no registration required.

I want to use with JavaScript and Drupal.t() the equivalent of format_interval().

With PHP I would use the following code.

print t("!date ago", array("!date" => format_interval(time() - $lastActivity, 1)));

What would the equivalent in JavaScript be?

share|improve this question
 
The t method is a Drupal text sanitizing and translating equivalent to the t() PHP function from Drupal core. –  Ayesh K May 20 '13 at 17:02
add comment

migrated from stackoverflow.com May 20 '13 at 18:51

This question came from our site for professional and enthusiast programmers.

2 Answers

Drupal doesn't implement a JS version of format_interval(); this is a rough (minimally tested) port:

Drupal.formatInterval = function(interval, granularity, langcode) {
  granularity = typeof granularity !== 'undefined' ? granularity : 2;
  langcode = typeof langcode !== 'undefined' ? langcode : null;

  var units = {
    '1 year|@count years': 31536000,
    '1 month|@count months': 2592000,
    '1 week|@count weeks': 604800,
    '1 day|@count days': 86400,
    '1 hour|@count hours': 3600,
    '1 min|@count min': 60,
    '1 sec|@count sec': 1
  },
  output = '';

  for (var key in units) {
    var keys = key.split('|'); 
    var value = units[key];
    if (interval >= value) {
      output += (output.length ? ' ' : '') + Drupal.formatPlural(Math.floor(interval / value), keys[0], keys[1], {}, { langcode: langcode });
      interval %= value;
      granularity--;
    }

    if (granularity == 0) {
      break;
    }
  }

  return output.length ? output : Drupal.t('0 sec', {}, { langcode: langcode });
}

Some random results using the above (they seem to match up to the PHP function as expected):

  • 3643 => 1 hour 43 sec
  • 92900 => 1 day 1 hour
  • 2592000 => 1 month
  • 9331200 => 3 months 2 weeks
  • 297605232 => 9 years 5 months
share|improve this answer
add comment

Clives implementation is doing well. However, Drupals javascript aggregator needs to parse all javascript files for translatable strings. As Clive uses dynamic values for Drupal.formatPlural this won't work here.

So here's another implementation with working translation:

Drupal.formatInterval = function(interval, granularity) {
  granularity = typeof granularity !== 'undefined' ? granularity : 2;    
  output = '';

  while (granularity > 0) {
    var value = 0;
    if (interval >= 31536000) {
      value = 31536000;
      output += (output.length ? ' ' : '') + Drupal.formatPlural(Math.floor(interval / value), '1 year', '@count years');
    }
    else if (interval >= 2592000) {
      value = 2592000;
      output += (output.length ? ' ' : '') + Drupal.formatPlural(Math.floor(interval / value), '1 month', '@count months');
    }
    else if (interval >= 604800) {
      value = 604800;
      output += (output.length ? ' ' : '') + Drupal.formatPlural(Math.floor(interval / value), '1 week', '@count weeks');
    }
    else if (interval >= 86400) {
      value = 86400;
      output += (output.length ? ' ' : '') + Drupal.formatPlural(Math.floor(interval / value), '1 day', '@count days');
    }
    else if (interval >= 3600) {
      value = 3600;
      output += (output.length ? ' ' : '') + Drupal.formatPlural(Math.floor(interval / value), '1 hour', '@count hours');
    }
    else if (interval >= 60) {
      value = 60;
      output += (output.length ? ' ' : '') + Drupal.formatPlural(Math.floor(interval / value), '1 min', '@count min');
    }
    else if (interval >= 1) {
      value = 1;
      output += (output.length ? ' ' : '') + Drupal.formatPlural(Math.floor(interval / value), '1 sec', '@count sec');
    }

    interval %= value;
    granularity--;
  }

  return output.length ? output : Drupal.t('0 sec');
}
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.