Date and time

From Habari Project

Jump to: navigation, search


You can customize the way date and time are displayed. The function we're looking at is <?php $post->pubdate->out(); ?> which outputs the date in the same time format you specified in your Habari options. Once you set a date and time under options this is applied globally but you can still specify a different format for single pages inside your theme.

Examples of different formats for time and dates are:

  • l, F j, Y -> Friday, September 24, 2010
  • m/j/y g:i A –> 03/12/97 5:35 PM
  • j F Y –> 12 August 1995
  • Y – 2010
  • o-m-d –> 2010-12-25 (ISO date format)

We are going to combine various part of the date in a string to generate a date in a format you like. The parameters we can use are those of PHP's date function. This is a list of all the accepted parameters from the website.
For the purpose of this example we will be editing home.php to create a date like 28 September 2010. You can change <?php echo $post->pubdate->out(); ?> to <?php echo $post->pubdate->out( 'j F Y' ); ?> where j represents the day of the month without leading zeros, F is a full textual representation of the month, such as January or March and Y is a full numeric representation of a year.

If you want to style each component of the date individually you could also write:
<?php echo $post->pubdate->text_format('<span class="day">{d}</span> <span class="month">{M}</span> <span class="year">{y}</span>' ); ?>

Using date and time to move posts

Posts in Habari do not only have a publication date, they have three different types of date. It is important to understand all of them in order to display posts in the order you want to. These values are the publication date (pubdate), the updated date (updated) and the modified date (modified).

When a post is initially published, all three values are the same. The pubdate will save this point in the lifetime of a post - it is never changed again from now on.

When this post is updated later, you can decide if the change you make is a big one or not. For Habari, this means: You have to decide if the post should move to the top in your feed. By default, all posts that are published for the first time are displayed on top (and this can only be changed by using a plugin), and posts that are updated after publishing are not moved. You can change the behaviour for updated posts by unchecking the "minor edit" checkbox in the post settings before clicking the save button. If you do so, the updated and modified fields will change - if not, only the modified field will change.

Please note that the updated field only influences the post order in the atom feed. Most themes, including all the ones shipped with Habari, sort their posts by pubdate. So if you want to move a post up to the top on your home page, too, you have to use a plugin or modify your theme.

There is a post in Google Groups giving an every-day example on how to use these values. Please note that the example only refers to the feed, not to the behaviour of the theme on your home page as described above.

Personal tools