Post Date Formatting

2 views
Skip to first unread message

insidertravels

unread,
Jun 21, 2009, 10:30:39 PM6/21/09
to habari-dev
I just joined this group today. I am used to Textpattern, so the way
Habari handles theme customization is counterintuitive to me. Plus,
the documentation is far from complete.


My first question is how do I format the post date as "X days ago" the
way it is possible to do in Textpattern?

Owen Winkler

unread,
Jun 21, 2009, 11:36:05 PM6/21/09
to habar...@googlegroups.com

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

insidertravels

unread,
Jun 21, 2009, 11:43:22 PM6/21/09
to habari-dev


On Jun 21, 8:36 pm, Owen Winkler <epit...@gmail.com> wrote:

>
> public function filter_post_pubdate_ago($pubdate) {
>    $days_ago = round((HabariDateTime::date_create()->int -
> $pubdate->int) / 86400);
>    return "{$days_ago} days ago";
>
> }
>

Thanks. =)

So, is this exactly, in its entirety, the snipped I am supposed to put
in theme.php file, or is there something else? For the record, I am
not a programmer, so I can only write basic PHP.

And, can I do the same thing with the dates in the comments?

Owen Winkler

unread,
Jun 21, 2009, 11:48:49 PM6/21/09
to habar...@googlegroups.com
insidertravels wrote:
>
>
> On Jun 21, 8:36 pm, Owen Winkler <epit...@gmail.com> wrote:
>
>> public function filter_post_pubdate_ago($pubdate) {
>> $days_ago = round((HabariDateTime::date_create()->int -
>> $pubdate->int) / 86400);
>> return "{$days_ago} days ago";
>>
>> }
>>
>
> Thanks. =)
>
> So, is this exactly, in its entirety, the snipped I am supposed to put
> in theme.php file, or is there something else? For the record, I am
> not a programmer, so I can only write basic PHP.

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


insidertravels

unread,
Jun 22, 2009, 8:38:13 PM6/22/09
to habari-dev
Ugh. I have no idea how to do any of this. I am unable to locate any
useful documentation. The wiki has only very basic information.

Michael C. Harris

unread,
Jun 22, 2009, 10:04:01 PM6/22/09
to habar...@googlegroups.com
2009/6/23 insidertravels <postm...@insidertravels.com>:
> Ugh. I have no idea how to do any of this. I am unable to locate any
> useful documentation. The wiki has only very basic information.

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

Arthus Erea

unread,
Jun 22, 2009, 10:06:49 PM6/22/09
to habar...@googlegroups.com
OK, I'll try to clarify a little.

First off, you need to create a custom theme class, as described here: http://wiki.habariproject.org/en/Customizing_Theme_Behavior

Essentially, you need to create a file named "theme.php" in your theme
directory with this content: http://pastie.textmate.org/521061

Finally, in the "entry.single.php" template just change "<?php echo
$post->pubdate; ?>" to "<?php echo $post->pubdate_ago; ?>" and the
custom date will be output.

Hope that helps.

insidertravels

unread,
Jun 22, 2009, 10:15:20 PM6/22/09
to habari-dev


On Jun 22, 7:04 pm, "Michael C. Harris" <michael.twof...@gmail.com>
wrote:
> 2009/6/23 insidertravels <postmas...@insidertravels.com>:

>
> 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 Universityhttp://twofishcreative.com/michael/blog
> IRC: michaeltwofish #habari


Thank you. =)

insidertravels

unread,
Jun 22, 2009, 10:16:04 PM6/22/09
to habari-dev


On Jun 22, 7:06 pm, Arthus Erea <arthus.e...@gmail.com> wrote:
> OK, I'll try to clarify a little.
>
> First off, you need to create a custom theme class, as described here:http://wiki.habariproject.org/en/Customizing_Theme_Behavior
>
> Essentially, you need to create a file named "theme.php" in your theme  
> directory with this content:http://pastie.textmate.org/521061
>
> Finally, in the "entry.single.php" template just change "<?php echo  
> $post->pubdate; ?>" to "<?php echo $post->pubdate_ago; ?>" and the  
> custom date will be output.
>
> Hope that helps.
>


Yes. Thanks. =)
Reply all
Reply to author
Forward
0 new messages