In Habari, all of the posts are Post object instances. The pubdate
property of the Post object contains the date of the post in the form of
a HabariDateTime object.
The HabariDateTime object has a method, format(), that can be used to
output the date however you want.
For example:
echo $post->pubdate->format('Y-m-d');
format() uses the same formatting strings as PHP's date() function.
Or, you can use HabariDateTime's text_format() method:
echo $post->pubdate->text_format('In the year {Y}...');
This method converts the letter inside the {} into the date part
specified, also like in PHP's date() function, but this lets you put
more text in the string.
Unfortunately, there's no "X days ago" function that I'm aware of, but
you can get the date as an integer (unix timestamp in seconds) and do
some math:
$days_ago = round((HabariDateTime::date_create()->int -
$post->pubdate->int) / 86400);
Knowing that, you can do some crazy stuff...
If you put this in your custom theme class, you can format any
$post->pubdate for output as "X days ago" just by referencing
$post->pubdate_ago instead:
public function filter_post_pubdate_ago($pubdate) {
$days_ago = round((HabariDateTime::date_create()->int -
$pubdate->int) / 86400);
return "{$days_ago} days ago";
}
What does this do? In short, it's a *filter* that affects the *Post*
object's *pubdate* field when it has the suffix *ago*. Every time the
theme tries to obtain that value, it puts the actual value through this
function and uses the returned result instead.
So then when you want to output "X days ago", you just echo
$post->pubdate_ago instead of echo $post->pubdate->format('Y-m-d') or
having to run that comptation every time or call an additional function
in your theme. You could probably even augment that basic function to
display years, months, moon cycles, or what have you.
Owen
No, it's not the whole thing. You need a custom theme class if you
don't already have one.
Your best bet is to copy one of the existing theme.php files from one of
the system themes, and then add this method to the theme's class -- that
is, inside the definition of the class, not just at the end of the file.
> And, can I do the same thing with the dates in the comments?
I think so, but you need to name the function differently for that (add
an additional method to the class):
public function filter_comment_date_ago($pubdate) {
And the rest is the same. You can actually use the same function for
both of these, but you'd need to register some plugin hooks manually,
and it's not as easy as what I've described.
Owen
Are you creating a theme from scratch or using another theme as a base
? If you're using another theme as a base, there will already be a
theme class in theme.php. Just put the post pubdate function Owen
wrote you above into the theme class. If you want the same format for
comments, copy the function again and change the name.
If you're creating a theme from scratch, do as Owen suggests and copy
theme.php from an existing theme. Or you can create your own, as
described on this page,
http://wiki.habariproject.org/en/Customizing_Theme_Behavior.
The wiki is a work in progress, and we recognise that there are some gaps.
--
Michael C. Harris, School of CS&IT, RMIT University
http://twofishcreative.com/michael/blog
IRC: michaeltwofish #habari