8

I would like to use the Timeago module as a date format. However, when the time ago has exceeded 24h, I would like it to show another format (e.g. 4 Feb, 2013) much like Twitter or Dribbble use.

I tried to tweak the code, but my skills let me down :/

Is there possibly a better module for this cause? Or do I have to make my own?

I found this code that shows how I want it to work, however I have no idea how to implement this to drupal.

Any help is appreciated, thanks.

  • This would be a great feature request for the Timeago module if it's not already been requested. – beth Feb 4 '13 at 0:24
  • @beth I just looked through the issues for the module and it doesn't seem to have been requested. I will create an issue tomorrow (no time for it today) unless you want to do it :) – Alex Feb 4 '13 at 0:29
  • What tweaks did you attempt to do to the code? Where is this date appearing on your site? – beth Feb 4 '13 at 1:04
  • @beth I tried to make the code in the jquery.timeago.js file only run if the seconds variable was less than 86400 (24h). However, I have to make the whole module to not run, otherwise it won't show the other formats, because it is still overriding them. – Alex Feb 4 '13 at 8:33
  • I created an issue that can be found here – Alex Feb 4 '13 at 8:41
1

Do users really sit on a page for a long enough time, that these dates need to be updated via javascript? Most of them will click on something and end up reloading the whole page at some point. So perhaps, a php solution is an option too?

You could achieve the php solution by using the Custom Formatters module.

If you create a new php type custom formatter, with the following code (make sure it's a datestamp type):

$element = array();
foreach ($variables['#items'] as $delta => $item) {
  $unixstamp = $item['value'];
  $time_since = mktime() - $unixstamp;
  if ($time_since < 86400) {
    $date_str = format_interval($time_since);
  }
  else {
    $date_str = format_date($unixstamp, 'custom', 'jS F Y');
  }

  $element[$delta] = array(
    '#type' => 'markup',
    '#markup' => $date_str,
  );
}
return $element;

When creating the formatter, make sure you select field type 'datestamp'. Once the formatter is created, then in the manage display tab of your content type, set the field to use this formatter.

If you don't have your date stored as a separate field, you might be able to apply the custom formatter to the node modified time, by installing the Display Suite module.

If you don't want to use any of these modules but want to hack some php into your theme or something, you can still use the same logic above with format_interval and format_date functions.

Hope that can help.

0

Wherever it is that you are using timeago to show a formatted date, will a snippet like the one below do the trick for you?

// Assume $timestamp has the raw Unix timestamp that I'd like to display using
// the "timeago" date format supposing it is within the last 24 hrs and another date
// format - "my_date_format" otherwise.
$rule = ((REQUEST_TIME - $timestamp) <= 24 * 60 * 60);
$fallback = format_date($timestamp, 'my_date_format');
return ($rule ? timeago_format_date($timestamp, $fallback) : $fallback);
  • Is this supposed to be applied to a .module file? I can't really figure out where to put it. – Alex Feb 4 '13 at 12:22
  • You would need to find where in the timeago .module file the new dates are applied and then you could attempt what @Amarnath was suggestion, or something similar, like an if statement wrapping around the actual application of the new dates and the condition being some math saying if the date is greater than 24 hours ago, do this. – CR47 Sep 19 '13 at 15:00
0

Create A MyCustom Module

myCustom.module contains

/**
 * Implements hook_date_formats().
 */
function myCustom_date_formats() {
  $formats = array('g:i a', 'H:i', 'M j', 'j M', 'm/d/y', 'd/m/y', 'j/n/y', 'n/j/y');
  $types = array_keys(myCustom_date_format_types());
  $date_formats = array();
  foreach ($types as $type) {
    foreach ($formats as $format) {
      $date_formats[] = array(
        'type' => $type,
        'format' => $format,
        'locales' => array(),
      );
    }
  }
  return $date_formats;
}

/**
 * Implements hook_date_format_types().
 */
function myCustom_date_format_types() {
  return array(
    'myCustom_current_day' => t('MyCustom: Current day'),
    'myCustom_current_year' => t('MyCustom: Current year'),
    'myCustom_years' => t('MyCustom: Other years'),
  );
}

/**
 * Formats a timestamp according to the defines rules.
 *
 * Examples/Rules:
 *
 * Current hour: 25 min ago
 * Current day (but not within the hour): 10:30 am
 * Current year (but not on the same day): Nov 25
 * Prior years (not the current year): 11/25/08
 *
 * @param $timestamp
 *   UNIX Timestamp.
 *
 * @return
 *   The formatted date.
 */
function myCustom_format_date($timestamp) {
  if ($timestamp > ((int)(REQUEST_TIME / 3600)) * 3600) {
    return t('@interval ago', array('@interval' => format_interval(abs(REQUEST_TIME - $timestamp), 1)));
  }
  if ($timestamp > ((int)(REQUEST_TIME / 86400)) * 86400) {
    return format_date($timestamp, 'myCustom_current_day');
  }
  if ($timestamp > mktime(0, 0, 0, 1, 0, date('Y'))) {
    return format_date($timestamp, 'myCustom_current_year');
  }
  return format_date($timestamp, 'myCustom_years');
}

Create myCustom.install & it contains

/**
 * @file
 * Install file for myCustom.module
 */

/**
 * Implements hook_install().
 */
function myCustom_install() {
  // Define default formats for date format types.
  variable_set("date_format_myCustom_current_day", 'g:i a');
  variable_set("date_format_myCustom_current_year", 'M j');
  variable_set("date_format_myCustom_years", 'n/j/y');
}

/**
 * Implements hook_uninstall().
 */
function myCustom_uninstall() {
  variable_del('date_format_myCustom_current_day');
  variable_del('date_format_myCustom_current_year');
  variable_del('date_format_myCustom_years');  
}

Usage :

echo myCustom_format_date(1392532844);
  • 2
    Hello. Could you post an explanation? This site is meant for answers, not for code. – Mołot Feb 16 '14 at 8:14
  • Yes ofcourse. I will care it for further answer. – Rupesh Feb 16 '14 at 11:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.