Jump to content

Blog posts - today/yesterday/x days ago problem


SamC
 Share

Recommended Posts

I'm trying to put a timestamp next to posts to show how long ago they were published. Is there something obvious I'm missing here as the only times I seem to be able to get are 'Today', 'Yesterday' and 3+ days ago, never '2 days ago'.

<ul class="list-unstyled">

        <?php
          $entries = $pages->find("template=blog-entry, sort=-postDate, limit=5");
          foreach ($entries as $entry):

            $timeStampNow = $datetime->date($format = "ts");
            // postDate is a Datetime field on the blog-entry template
            $timeStampPostDate = $entry->getUnformatted("postDate");
            $difference = $timeStampNow - $timeStampPostDate;
            
            if ($difference < 86400) {
              $daysAgo = "Today";
            }
            else if ($difference > 86400 && $difference < (86400 * 2)) {
              $daysAgo = "Yesterday";
            }
            else {
              $daysAgo = date("j", $difference) . " days ago";
            }
          ?>
            <li class="py-1"><a href="<?= $entry->url; ?>"><?= $entry->title; ?></a> - <?= $daysAgo; ?></li>

          <?php endforeach; ?>

        </ul>

Maybe I'm not taking into consideration the actual time of day they were posted, maybe the code above does work, but I've never been able to output '2 days ago' thus far. Any advice would be awesome, thanks.

[BASICS]

59d7a27fa50e0_ScreenShot2017-10-06at16_32_25.thumb.png.61bc5705e3c5dc5eddeea179abe3fa25.png

[DETAILS]

59d7a28f93a12_ScreenShot2017-10-06at16_32_35.thumb.png.2f93fbd1d6b56da4c867fe693dcd3cf0.png

[INPUT]

59d7a29db70f4_ScreenShot2017-10-06at16_33_01.thumb.png.70ac81ad3df877373702eafe3a4a6cc1.png

[OUTPUT]

59d7a2a868ab0_ScreenShot2017-10-06at16_33_48.thumb.png.154e7643ac118393cea29bedba150c18.png

Link to comment
Share on other sites

Ok, working now, I changed the date picker to include an actual time too and tested:

59d7b10b41c4e_ScreenShot2017-10-06at17_34_31.thumb.png.b9f81681f0ca4056575ba8c384c6f9b8.png

// blog-index.php
<ul class="list-unstyled">

  <?php
    $entries = $pages->find("template=blog-entry, sort=-postDate, limit=5");
    foreach ($entries as $entry):
      $daysAgo = $datetime->relativeTimeStr($entry->getUnformatted("postDate"));
  ?>

  <li class="py-1"><a href="<?= $entry->url; ?>"><?= $entry->title; ?></a> - <?= $daysAgo; ?></li>

  <?php endforeach; ?>

</ul>

...and:

59d7b11273742_ScreenShot2017-10-06at17_36_07.thumb.png.58672b0f07132e72d7b3f502f4f97cb6.png

Gotta start checking that API reference more often.

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