Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
By jermb on
How can this be done? is there variations to the $date format?
Comments
Heaps of variations on the
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
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
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
Awesome I think I understand now how to do it. Thank you very much.
Time ago Module
If anybody is still looking :)
https://www.drupal.org/project/timeago
WTF?! You answered to 12
WTF?! You answered to 12 years old thread just to provide a link to module which is compatible with drupal 7 and has no drupal 8 version. Also lack of drupal 4 version makes no sense if we take into account age of this thread.
Your point is!!!!
How do you think I found this page? So don't you think it makes sense to save the next person the trouble of looking. Bye the way lots of Drupal 7 websites still running.
FYI if you're still looking
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):
Don't forget the closing ?> if left out above.
Here is how you can get a
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.
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!
There is a Drupal function that does that
http://api.drupal.org/api/4.7/function/format_interval
Caroline
11 heavens
Update 'Time Ago'
Thanks Caroline, I didn't even think to look. :)
For those curious here's what I use now -
And the Drupal 5 function page is over here - http://api.drupal.org/api/5/function/format_interval
Thanks a lot! I can confirm
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
I have implemented this in my Drupal 6 instance, works like a charm, thanks!
another example, this one is
another example, this one is straight out of my node templates
now I have the "posted blahblah time ago" text in $time_ago.
Mine are all coming up as '40
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
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.
golly... thanks!
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
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
time()-
Sounds like you need to add time()- like so
Hey, code (<?php print
Hey, code (
print format_interval(time()-$node->created);
) posted by benleivian works perfectly in drupal 6. ThanksGood Approach. Works for me.
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.
D6
No more of this in D6 with the Submitted_by module.
http://drupal.org/project/submitted_by
http://drupal.org/node/506926
Simplest: <?phpfunction
Simplest:
The Answer!
Here is a complete answer, and "ago" is removed with no core hacking! Toss this in your template.php
Ryan aka Devkinetic
Sr. Web Developer
Does this work for Drupal 7?
Does this work for Drupal 7?
Its not working for me, how
Its not working for me, how can I apply this to my ZEN subtheme?
D7 Example
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.incand
function theme_date_display_interval($vars)
- from date.themeFor Drupal 7:
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'?>
Lets not write crappy non-I18N code shall we?
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 ;)
Can't get this to work
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?
Got this to work
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.
For comments?
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