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

  • 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....


    pages/home.php...



    layouts/global/time_ago.php...


    templates/blog/home_main_headlines.html...


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


    Glen

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

    Code
    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. ]);
  • Post by Glen ().

    This post was deleted by the author themselves ().