Jump to content

How to display relative dates in PW


hafa
 Share

Recommended Posts

How to display relative dates like the ones we often see in Tumblogs and other social networks?

Examples:

Just now

2 seconds ago
5 minutes ago
2 hours ago

Today

Yesterday
2 days ago
2 weeks ago

I have a feeling it would be kind of tricky, but with PW, who knows :D

Link to comment
Share on other sites

Take a look at PHP's strtotime() - it should handle every you need.

http://us.php.net/manual/en/function.strtotime.php

Let us know if you need any help making it work.

EDIT: I should also add that you will probably want to make use of PHP's date() to then format the results from unix time to something human readable: http://php.net/manual/en/function.date.php

Link to comment
Share on other sites

@adrian - Thanks for the link! 
 
So should I create a functions.php file containing the function wireRelativeTimeStr?
 
And in my template I just do what I normally do?
 
<span class='date'>{$entry->date}</span>

Is that correct?

Link to comment
Share on other sites

No, it means this is already supported on global scope in PW:

wireRelativeTimeStr($timestamp);

So try this in your template file to get started:

echo "This page was created " . wireRelativeTimeStr($page->created);


 

Link to comment
Share on other sites

Hey @apeisa,

I'd like to display relative dates in blog posts. Is the code below the best way to do something like this?

<h1>{$blogPost->title}</h1>
<p class='date'>Posted <?php echo wireRelativeTimeStr($blogPost->date); ?></p>
<p>{$blogPost->content}</p>
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...