Hello there,

I'm looking to add a time stamp to comments which basically reads the following.

Username, 37 seconds ago.

The default code in comment.tpl.php is:

<div class="submitted">
    <?php
      print t('Submitted by !username on !datetime.',
        array('!username' => $author, '!datetime' => $created));

Is it a simple case of replacing !datetime with <?php print $submitted; ?>? I'm having trouble getting this to work. Many thanks for any suggestions.

Comments

nvl.sateesh’s picture

Ok. here is what you can do:

First, remove the following code from your theme:


  <?php if ($submitted): ?>
    <span class="submitted"><?php print $submitted; ?></span>
  <?php endif; ?>

Now, you can compile the time lapsed from the time of adding that comment like this:


print localised_date($comment->created);

Please the following function either in your template.php file or any of your custom modules if you have one:

function localised_date($timestamp){

  $diff = time() - $timestamp;
  $rest = ($diff % 3600);
  $restdays = ($diff % 86400);
  $restweeks = ($diff % 604800);
  $weeks = ($diff - $restweeks) / 604800;
  $days = ($diff - $restdays) / 86400;
  $hours = ($diff - $rest) / 3600;
  $seconds = ($rest % 60);
  $minutes = ($rest - $seconds) / 60;

  if ($timestamp == '')
    return $lang_common['Never'];
  if($weeks > 1)
    return $date."$weeks weeks ago";
  elseif($days > 1)
    return $date."$days days ago";
  elseif($hours > 1)
    return $date."$hours hours ago";
  elseif ($hours == 1)
    return $date."1 hour, $minutes mins ago";
  elseif ($minutes == 0)
    return $date."$seconds seconds ago";
  elseif ($minutes == 1)
    return $date."1 minute ago";
  elseif ($seconds < 60)
    return $date."$minutes mins ago";
}

Sateesh Nutulapati
Devops Solutions Architect at New Target, Inc.

TGReaver’s picture

Thank you for your quick response. I've added the function to my template.php file, is it now just a case of replacing:

<?php if ($submitted): ?>
    <span class="submitted"><?php print $submitted; ?></span>
  <?php endif; ?>

With print localised_date($comment->created);?

nvl.sateesh’s picture

I suppose you also wanted the user name? in that case, you have to add that as well.

<?php
$username  = get_username($comment->uid);
$timeframe  = localized_date($comment->created);

print $username .' '. $timeframe;
?>

with this, you will also have to include the get_username function in your template.php file:

<?php
function get_username($uid){
  if(is_numeric($uid) && $uid!=0){
    $username = db_result(db_query("SELECT name FROM {users} WHERE uid=%d LIMIT 1", $uid));
    return $username;
  }else{
    return t('Anonymous');
  }
}
?>

I am not sure if my seniors will agree with this method. but you can try it for now, until someone comes and bashes you and me ;-)

also, check if $comment->created prints the date in the first place... just to make sure it returns the timestamp something like 123434281

<?php
print $comment->created;
?>

Sateesh Nutulapati
Devops Solutions Architect at New Target, Inc.

TGReaver’s picture

Hi there,

I've added the code you provided me with to my template.php file, under comments. (Is this correct?) I also added the following code to my comment.tpl.php file to check if the time stamp would print.

<?php
print $comment->created;
?>

Nothing seems to be printing at all, no errors or anything within my browser. I read through the guides SweeneyTodd kindly directed me to and I understand this process a lot more, it should in theory be working?

nvl.sateesh’s picture

of course. what sweeny said is something that already is working in the forum module... if you are comfortable with that logic, that is more advisable...

Sateesh Nutulapati
Devops Solutions Architect at New Target, Inc.

nvl.sateesh’s picture

by the way, i copied that function from the localised_date following link:

http://forum.coppermine-gallery.net/index.php?topic=44280.0

Sateesh Nutulapati
Devops Solutions Architect at New Target, Inc.

SweeneyTodd’s picture

Look at the core forum module. They have an example there.

In forum-submitted.tpl.php they just have:

<?php if ($time): ?>
  <?php print t(
  '@time ago<br />by !author', array(
    '@time' => $time,
    '!author' => $author,
    )); ?>
<?php else: ?>
  <?php print t('n/a'); ?>
<?php endif; ?>

$time is calculated in the preprocess function (function template_preprocess_forum_submitted in forum.module)

function template_preprocess_forum_submitted(&$variables) {
  $variables['author'] = isset($variables['topic']->uid) ? theme('username', $variables['topic']) : '';
  $variables['time'] = isset($variables['topic']->timestamp) ? format_interval(time() - $variables['topic']->timestamp) : '';
}

You should be able to override your preprocess function in your theme to add a time variable and reference it in your template.

It depends on how much experience you have to how easy it is.

Let me know if you need more help.

tinny’s picture

Hi, your comment help me out a lot. Here is what I did:

The following applies to D6 (not sure about d7)

So what I wanted was "This comment was posted X hrs and x minutes ago"

1) Set up a $timeago variable to be used in your .tpl.php files.
To do this, first go here: http://drupal.org/node/223430
You need to work out the naming convention for your preprocessor. In my case, I used #9 themeName_preprocess because im going to put this code in my template.php file in my custom sub theme.

2) In template.php

<?php
function themeName_preprocess(&$variables) {
  $variables['timeago'] = isset($variables['comment']->timestamp) ? format_interval(time() - $variables['comment']->timestamp) : '';
}

Replace themeName with the name of your theme.

2.1) Where does $variables['comment']->timestamp come from?
Put the following into your comment.tpl.php <?php dsm($variables) ?> (must have devel module enabled). Now go to a page on your website where (a small number of) comments have been posted. From there you'll see the variables comment, node, links, etc. Click on comment and you'll see timestamp.

2.2) Where does format_interval come from?
http://api.drupal.org/api/drupal/includes--common.inc/function/format_in...

2.3) Where does time() come from?
http://php.net/manual/en/function.time.php

3) In comment.tpl.php
Now you can use the variable $timeago which you just created. print $timeago;