hafa Posted May 19, 2013 Share Posted May 19, 2013 How to display relative dates like the ones we often see in Tumblogs and other social networks? Examples: Just now 2 seconds ago5 minutes ago2 hours ago Today Yesterday2 days ago2 weeks ago I have a feeling it would be kind of tricky, but with PW, who knows Link to comment Share on other sites More sharing options...
adrian Posted May 19, 2013 Share Posted May 19, 2013 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 More sharing options...
WillyC Posted May 19, 2013 Share Posted May 19, 2013 echo wireRelativeTimeStr($page->date_field); 7 Link to comment Share on other sites More sharing options...
hafa Posted May 19, 2013 Author Share Posted May 19, 2013 @WillyC - Should I include it somewhere in my frontend template? <span class='date'>{$entry->date}</span> Thanks guys. Link to comment Share on other sites More sharing options...
adrian Posted May 19, 2013 Share Posted May 19, 2013 Willy, thanks for the heads up about that PW function. hafa - here is the details of that function: https://github.com/ryancramerdesign/ProcessWire/blob/dev/wire/core/Functions.php I had to look it up myself Link to comment Share on other sites More sharing options...
WillyC Posted May 19, 2013 Share Posted May 19, 2013 @WillyC - Should I include it somewhere in my frontend template? you.will find only what you bring in 3 Link to comment Share on other sites More sharing options...
hafa Posted May 19, 2013 Author Share Posted May 19, 2013 @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 More sharing options...
apeisa Posted May 19, 2013 Share Posted May 19, 2013 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 More sharing options...
hafa Posted May 19, 2013 Author Share Posted May 19, 2013 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 More sharing options...
apeisa Posted May 19, 2013 Share Posted May 19, 2013 If you have datefield called date, then yes, looks good to me. Why not try and see how it works? (never used wireRelativeTimeStr myself) Link to comment Share on other sites More sharing options...
adrian Posted May 19, 2013 Share Posted May 19, 2013 BTW hafa - please ignore my original answer - I really didn't read your question properly at all Link to comment Share on other sites More sharing options...
hafa Posted May 20, 2013 Author Share Posted May 20, 2013 @adrian - No worries buddy @apeisa - Yup, going to later this evening. 1 Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now