Date format to 'xx time ago' for node teasers

jermb - May 3, 2006 - 06:05

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

Heaps of variations on the

gollyg - May 3, 2006 - 07:42

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

jermb - May 3, 2006 - 16:03

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

gollyg - May 3, 2006 - 19:49

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

jermb - May 4, 2006 - 14:30

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

FYI if you're still looking

krisvannest - January 12, 2007 - 23:17

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

drenei - February 8, 2007 - 15:39

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!

Update 'Time Ago'

drenei - March 8, 2007 - 15:44

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

xushi - December 27, 2007 - 08:32

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

Awesome

Roelven - October 20, 2008 - 14:13

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

another example, this one is

danielb - October 28, 2008 - 01:53

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.

 
 

Drupal is a registered trademark of Dries Buytaert.