Creating 'Time Ago' Blog Post Dates

  • Hi Everyone.

    I have a news based client / website for which I am using the blog app for and I am wanting to have a more dynamic way of displaying when the post was published.

    You see this method quite a lot on most news sites, for example on BBC News, where instead of displaying the date itself, you display "4 hours ago" or "2 days ago" etc.

    I'm guessing the best way to do it is to write a php script to generate to output and then pass that via PerchSystem::set_var('name', 'value') , but I was just wondering if anyone done this on Perch or would know the best way to achieve this?

    Many thanks


  • Glen

    Changed the title of the thread from “Creating 'Dynamic' Blog Post Dates” to “Creating 'Time Ago' Blog Post Dates”.
  • Thanks Byron Fitzgerald for your reply.

    I have now got the php working but I still can't seem to pull in the blog post date from Perch. The output I get is "1 Jan 1970" but if I manually include a date it works perfectly....




    Any help to point me in the right direction would be must apricated.


  • To get the post date rather than using the data option you can use the each option like below.

    1. perch_blog_custom([
    2. 'sort' => 'postDateTime',
    3. 'sort-order' => 'DESC',
    4. 'count' => 3,
    5. 'template' => 'home_main_headlines.html',
    6. 'each' => function($post) {
    7. $post['postAgo'] = get_time_ago(strtotime($post['postDateTime']));
    8. return $post;
    9. },
    10. ]);
  • Hi Byron Fitzgerald - I hope you had a great Christmas & News Year?

    I was wondering if you could help me once again?

    I have copied over the timeAgo code to another website but this time into the perch_comments addon. I have copied everything across like for like but nothing is being displayed, would you mind please having a quick look to see what I've missed?...

    ...thank you sooooo much for your help. :)


  • This seems odd to me. Normally layouts are string literals pointing to a html file in templates.

    1. perch_layout('time_ago', array(
    2. 'newsDateTime' => date("j M Y", strtotime($newsDateTime)),
    3. ));
    1. perch_layout('layouts/_root_comment_date.html');

    Hi ian Hobbs

    You can pass variables into layouts just like you can with other regions...…nd-nav/layouts/variables/

    As mentioned, it works perfectly well on another site using the blog addon, for some reason I can’t get it to work on this site using the comments addon?


  • Hi Glen,

    Had a lovely break thanks, hope you did to.

    I think it might be because the comments app is a bit outdated and doesn't have the full available options when rendering the templates. I'm pretty sure the each option doesn't exists for perch_comments. You'll need to return all the comments, then loop through them, then pass to perch_template. Something like below should work

  • Hi Byron Fitzgerald - wow thank you :)

    Unfortunately, nothing is being displayed, I ran a debug which showed an array error but after fixing that nothing still displays. The updated code I am using is as follows...

    Time ago code...

    Comments code...

    Thank you so much :thumbup:

  • Think it's an issue with the array map, should probably be

    1. // Map the array like we do in the previously used each property
    2. $comments = array_map( function($post) {
    3. $post['postAgo'] = get_time_ago(strtotime($post['commentDateTime']));
    4. return $post;
    5. }, $comments);