Is there a simple way to group nodes by their creation date?

eaton - December 14, 2005 - 00:53

One of the basic features of most blog packages is breaking up lists of posts by the day they were posted on. Instead of listing the post-date on EVERY node teaser, they're displayed like so:

TUESDAY, DECEMBER 13th
> Node 1
> Node 2
> Node 3
MONDAY, DECEMBER 12th
> Node 4
> Node 5
> Node 6

And so on. Quite a few multi-user sites like metafilter.com display things this way, too. Is there a simple way to do this using PHPTemplate theming? I'm sure I'm missing something obvious...

complementing this question....

lzfranc - December 14, 2005 - 02:02

or showing in this condition?

DECEMBER
> Node1
> Node2
> Node3

NOVEMBER
> Node 4
> Node 5 ...

The Node Queue module does

venkat-rk - December 14, 2005 - 02:48

The Node Queue module does list nodes by group, but I am not sure it meets your exact requirement:
http://drupal.org/node/38678

No, Node Queue won't do

merlinofchaos - December 14, 2005 - 05:30

No, Node Queue won't do it.

Views can if you write a custom theme for the view.

-- Merlin

[Point the finger: Assign Blame!]
[Read my writing: ehalseymiles.com]

Example in cvslog module

nedjo - December 14, 2005 - 03:02

CVS log messages are grouped by date, see

http://drupal.org/cvs

You can see how this is done in the cvs.module.

See use of the _cvs_date() function, which tests each record to see if it has a new date and, if so, outputs the date.

Thanks!

eaton - December 14, 2005 - 05:51

That's a really nice compartmentalized function. I pulled it out and tweaked it a bit for easy use in a theme:

function date_header($timestamp, $format = 'F j, Y') {
  static $last;
  $date = format_date($timestamp, 'custom', $format);
  if ($date != $last) {
    $last = $date;
    return $date;
  }
}

If there's a new date, it returns the formatted string. If not, it returns nothing. It would probably be best used in a custom listing; sticking a call to this right in the node.tpl.php would get tricky, as it might show up in all sorts of listings where date-headers would be inappropriate. Thanks, though, for pointing me in the right direction.

--
Jeff Eaton | I heart Drupal.

Handy

C-Watootsi - March 18, 2006 - 22:43

Nice one Jeff -- I recently asked about this same issue (also thinking of metafilter) and was just pointed to your code. It's odd that Aggregator has this function by default, but not the "group blog" setup.

Hi may anyone tell me where

ilike - February 23, 2008 - 13:02

Hi may anyone tell me where to put the above function for calling later in the template. Just putting it in the template doesnt work since it will be executed once for every node and giving error since the function is already defined.

If someone is interested in

ilike - February 23, 2008 - 14:37

If someone is interested in doing this in drupal 5.7 i found that the following(probably terrible) code works in a template file.

<?php

       
       
global $forra;
       
$datum = format_date($node->created, 'custom', 'F j, Y');
        if (
$datum != $forra) {
        print
$datum;
       
$forra = $datum;
        }
       

   
   
?>

 
 

Drupal is a registered trademark of Dries Buytaert.