How can this be done? is there variations to the $date format?

Comments

Heaps of variations on the date format.
http://www.php.net/date

basically date formats a number representing the number of seconds since January 1 1970 00:00:00 GMT.
time() returns the value of that number for, well, the current time.

So by subtracting the number representing your node modified time from the current time and converting it from seconds into whatever you want, you can display it anyway you like. I don't know if there is a function in php/drupal to convert it to a nice format (eg 30 seconds ago, 2 hours ago) but forums and admin module to it so I guess it is probably there in the api? If not, wouldn't be hard to write.

Right. I was hoping to not have to modify the node module itself, and that there was an easy way to just mess with the theme...

If you are using php template then this can all be done in the theme. We are dealing with the node preview, so you will be editing your node.tpl.php file. This contains an if/else statement that test the page variable to see if we are looking at a node preview (ie a listing of nodes) or the full story.

You want to edit the preview. You can retrieve the numberic date modified value from the $node variable in the theme ie $node->changed. This returns the number that we were talking about - seconds since 1970. Then apply whatever formating you want via the methods mentioned above.

If you are doing it to several different fields/node types you may want to write a function and place it in the template.php or check out drupaldocs.org to see if there is a prebuilt Druapal function to do what you want.

There is very rarely a situation where hacking the core is a good idea.

Awesome I think I understand now how to do it. Thank you very much.

FYI if you're still looking for a solution, I took the above comments/supplied links and created the following code to insert into my node.tpl.module (4.7.x):

<?php
        $days
= floor((time() - strtotime($date))/86400);
        if (
$days == 0) {print("<b>just today!</b>");}
        elseif (
$days > 0) {print("<b>$days days ago</b>");}
        else {
$days = abs($days); print("<b>$days days from now</b>");}
       
?>

Don't forget the closing ?> if left out above.

Here is how you can get a 'time ago' format for any date on your website. (Code Taken from andrew macrobert's comment on php.net/manual/en/function.time.php.)

Step 1. Put the following function into your template.php file.

<?php
function ago($timestamp){
  
$difference = time() - $timestamp;
  
$periods = array("second", "minute", "hour", "day", "week", "month", "years", "decade");
  
$lengths = array("60","60","24","7","4.35","12","10");
   for(
$j = 0; $difference >= $lengths[$j]; $j++)
  
$difference /= $lengths[$j];
  
$difference = round($difference);
   if(
$difference != 1) $periods[$j].= "s";
  
$text = "$difference $periods[$j] ago";
   return
$text;
  }
?>

Step 2.

Replace any dates in your tpl.php files with ago([date]);

For example - To change comment date stamps you would replace the the format_date function with:
ago($comment->timestamp)

Hope that helps!

Thanks Caroline, I didn't even think to look. :)

For those curious here's what I use now -

<?php
function ago($timestamp){
  
$difference = time() - $timestamp;
  
$text = format_interval($difference, 1) . " ago";
   return
$text;
  }
?>

And the Drupal 5 function page is over here - http://api.drupal.org/api/5/function/format_interval

Thanks a lot!

I can confirm that drenei's code above works perfectly. No need to install a module.

Just insert the above function into your template.tpl.php, and edit your comment.tpl.php changing the lines as follows

<div class="submitted"><?php print t('On ') .  format_date($comment->timestamp, 'custom', 'F jS, Y'); ?> <?php print theme('username', $comment) . t(' says:'); ?></div>

to

<div class="submitted"><?php print t('Posted ') . ago($comment->timestamp) . ' by '; ?> <?php print theme('username', $comment); ?></div>
--
\/ushi - xushi.co.uk
/\ socialprotest.com

--
\/ushi - xushi.co.uk
/\

I have implemented this in my Drupal 6 instance, works like a charm, thanks!

~R.

another example, this one is straight out of my node templates

    <?php
    $interval
= date('U') - $node->created;
   
$hour = 60*60;
   
$day = 24*$hour;
   
$time_ago = t("posted ");
    if (
$interval < $hour) {
     
$time_ago .= t("a few moments ago");
    }
    elseif (
$interval < $day) {
     
$time_ago .= t("today");
    }
    elseif (
$interval < 2*$day) {
     
$time_ago .= t("yesterday");
    }
    else {
     
$time_ago .= format_interval($interval, 1) . t(' ago');
    }
?>

now I have the "posted blahblah time ago" text in $time_ago.

Mine are all coming up as '40 years ago' no matter what the date is... I've tried a few different combos of the template.php function code with various ways shown above to call the amount of time.. am I missing something here??

I would say the date you are passing the function is null. Null dates are interpreted as the beginning of the Linux epoch which just happens to be 40 years ago. Try printing out the value in your date variable to see what you are getting.

I will concentrate on passing a proper date.. thanks for the tip. Interesting factiod about the Linux epoch too.
The dates are for custom nodes, not comments btw... to be displayed within the node's page as well as two block views. Any tips on pulling a proper date? Right now the date displays correctly using $date on the node page... but apparently the function can't use this as is....

You can try changing the date display in "admin/settings/date-time/formats". This will be site wide, but you can add formats. It may not give you what you need.

For access to the raw data you need to go to the node object. There are two properties: "node->created" and "node->changed". These hold the actual linux timestamp (an integer rather than a readable date). You can print them out using the drupal function date_format() http://api.drupal.org/api/function/format_date/6

For the format you may be using custom, so this page should be useful http://au.php.net/manual/en/function.date.php

Finally, you probably want to provide the variable to your node template pre-rendered, so modify/add it in a preprocess hook for nodes, so look at http://api.drupal.org/api/function/template_preprocess_node/6

Sounds like you need to add time()- like so

<?php print format_interval(time()-$comment->timestamp,6); ?>

Hey, code (

<?php
print format_interval(time()-$node->created);
?>
) posted by benleivian works perfectly in drupal 6. Thanks

Just need to put this function in includes/common.inc instead of template.php
You can put this code just near to format_interval(). It is also for the same issue.

No more of this in D6 with the Submitted_by module.

http://drupal.org/project/submitted_by
http://drupal.org/node/506926

Simplest:

<?php
function ago($timestamp){
  return
format_interval(time() - $timestamp, 1). ' '. t('ago');
}
?>

Here is a complete answer, and "ago" is removed with no core hacking! Toss this in your template.php

<?php
function THEMENAME_date_time_ago($start_date, $end_date, $interval = 1) {
 
// If no date is sent, then return nothing
 
if (empty($start_date) || empty($end_date)){
    return
NULL;
  }
 
// Time to compare dates to
 
$now = date_format(date_now(), DATE_FORMAT_DATETIME);
 
$start = date_format($start_date, DATE_FORMAT_DATETIME);
 
$end = date_format($end_date, DATE_FORMAT_DATETIME);
 
// 1) The date is entirely in the future
 
if ($now < $start) {
    return
t('!time from now', array('!time' => date_format_interval($start_date, $interval)));
  }
 
// 2) Ongoing date
 
elseif ($now > $start && $now <= $end) {
   
//return t('Started !time ago', array('!time' => $dates['value']['interval']));
   
return t('ongoing');
  }
 
// 3) Date is in the past (format_interval added 'ago' to the value).
 
else {
    return
substr(date_format_interval($start_date, $interval), 0, -3);
  }
}
?>

Does this work for Drupal 7?

Everything I'm not, makes me everything I am

Its not working for me, how can I apply this to my ZEN subtheme?

First posting... I'm still a newish Drupal developer. Not sure why, but I had to pull two sections of code into my template.php to get it to work on D7. I still over-rode the date format in the time_ago function the same as Ryan.

function theme_date_time_ago($vars) - from theme.inc
and function theme_date_display_interval($vars) - from date.theme

No need to add a function to template.php (unless formatting for a date module field).

To show node time ago, add to node.tpl.php:

<?php echo 'Posted: ',format_interval(time()-$node->created),' ago'?>

To show comment time ago, add to comment.tpl.php:

<?php echo 'Posted: ',format_interval(time()-$comment->created),' ago'?>

It's not 1999 anymore. The proper code should look like this:

echo t('Posted: !interval ago', array('!interval' => format_interval(time() - $comment->created)))

I've wrapped this in a method to test (time() - $comment->created > 86400). For values older than one day ago I print the date with the normal format method; just like facebook ;)

I tried adding this to a custom code field in Display Suite and I couldn't get it to work. I was using $node->created instead of $comment->created, but it returned a value of 42 years ago, which suggests it's not seeing the created date. Any suggestions?

Just FYI, I got this to work. You need to replace $node->created with $entity->created, and select Display Suite Code as the filter, not PHP Code.

It would be great to be able to get this in the node teaser in 6.x but I don't know how to get that...

UPDATE - I got it working - see http://drupal.org/node/1795072

<?php if ($node->comment_count != '0') {echo t(' - Last comment !interval ago', array('!interval' => format_interval(time() - $node->last_comment_timestamp)));}