Hide div after certain time

Hi,

Iā€™m working on some kind of blog/archive and I was wondering if there is a way to display a Ā»newĀ« and Ā»editedĀ« div which disappears after a certain time.

I guess there is one but I have no clue how to do so :blush:

Iā€™m eager to hear your solutions
kind regards in advance
J

Hi!

I donā€™t know how familiar you are with PHP. But displaying a ā€œnewā€ badge of some sorts is pretty straight forward.
You will need a date field on the article that gets updated when saving/editing and a defined time span when articles are still considered ā€œnewā€.
In the articles template you will just need to check wether today is after the date field plus the time span.

If you need help with the actual code, just reply.

Best,
Thomas

Hi Thomas,

Iā€™m learning php with kirby, so there is a very bad base knowledge of it.

Thatā€™s what Iā€™ve found in a stack overflow, which should work:

<?php if ( time() - $data->time < 259200 ):?>
<div class="listing-time"><?php echo timespan($data->time); ?> since</div>
<?php endif;?>

Actually it seems quite logical but it doesnā€™t work. The first line reads:
if actual time ā€“ article time is smaller then 3 days: do something

instead of $date->time Iā€™ve put $article->date(ā€˜cā€™, ā€˜dateā€™)->time to fetch the article time.
Is this correct? What does <?php echo timespan($data->time); ?> mean?

Sorry for my lacking skills in php. :blush:

Unfortunately you do not link to your source, so itā€™s impossible to tell where the timespan stems from.

Here is a possible solution, guess, there are other ways of solving this:

<?php
$articles = $page->children()->visible()->filter(function($article) {
    $time = new DateTime('now'); 
    $published = new Datetime($article->date('Y-m-d')); 
    $interval = (int)$published->diff($time)->format('%a');
    if($interval < 3) {
      return $article;
    }
});
foreach($articles as $article) {
	echo $article->title();
}

?>

The above code uses a custom filter with a callback http://getkirby.com/docs/cheatsheet/pages/filter, and the php DateTime class http://php.net/manual/de/book.datetime.php