Display "NEW" badge on article

Hi all,

having just started out with Kirby and PHP in general I’m stuck at a (probably) simple task.
I want to display a badge on blog articles that are less or equal to 5 days old.

Right now I have it set up like this, which is pretty lame:

<?php foreach ($entries as $entry): ?>
  <article>
    <?php if ($currentDate === $entry->date()->toDate('d.m.y')): ?>
       New
    <?php endif ?>
  </article>
<?php endforeach ?>

$currentDate = date('d.m.y');

I tried following these instructions but with no success.

How can I display something if a date is today or up to five days ago?

Thanks!

1 Like

if (time() < $entry->date()->toDate() + 432000) compares the current Unix timestamp on the left (PHP function time()) with the Unix timestamp of the entry date plus 432000 (5 days x 86400 seconds/day) and validates to true if the former is smaller, i.e. the time has not yet passed that 5 day threshold.

…if the second-precision is not what you need, but a “five full calendar days”, it gets a tad more complex; a good starting point is php - Find if date is less than 7 days from now - Stack Overflow