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


gollyg’s picture

Heaps of variations on the date format.

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.

jermb’s picture

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...

gollyg’s picture

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 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.

jermb’s picture

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

krisvannest’s picture

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):

        $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.

drenei’s picture

Here is how you can get a 'time ago' format for any date on your website. (Code Taken from andrew macrobert's comment on

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

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:

Hope that helps!

drenei’s picture

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

For those curious here's what I use now -

function ago($timestamp){
   $difference = time() - $timestamp;
   $text = format_interval($difference, 1) . " ago";
   return $text;

And the Drupal 5 function page is over here -

xushi’s picture

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>


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

Roelven’s picture

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

danielb’s picture

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


    $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.

corypark’s picture

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??

gollyg’s picture

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.

corypark’s picture

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....

gollyg’s picture

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()

For the format you may be using custom, so this page should be useful

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

benleivian’s picture

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

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

Hey, code (

 print format_interval(time()-$node->created);
) posted by benleivian works perfectly in drupal 6. Thanks
govindtotla’s picture

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

Danny_Joris’s picture

No more of this in D6 with the Submitted_by module.

sobi3ch’s picture


function ago($timestamp){
  return format_interval(time() - $timestamp, 1). ' '. t('ago');
devkinetic’s picture

Here is a complete answer, and "ago" is removed with no core hacking! Toss this in your template.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);
dre2phresh’s picture

Does this work for Drupal 7?

beumont’s picture

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

heddn’s picture

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
and function theme_date_display_interval($vars) - from date.theme

Coupon Code Swap’s picture

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'?>

aaronaverill’s picture

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 ;)

RKopacz’s picture

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?

RKopacz’s picture

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.

HongPong’s picture

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

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