Hide text after a week from startdate


#1

Hi, how can I hide my text when it has passed one week from a specific date?
I`m using a datefield for my date.
Hope you understand me.


#2

You could filter what you display by date, see examples here: https://getkirby.com/docs/cookbook/filtering. Depending on you structure, you’d probably have to use a controller that redirects your page to another page to really make it inaccessible via URL once the date has passed.


#3

Does your datefield store the date you would like to remove the page or the publish date?

If it’s the publish date, you would need to compare it to todays date, and filter out if the difference is great enough. Theres some helpful stuff here.

Basically tweak this line from the cookbook:

$collection = page('blog')->children()->visible()->filterBy('date', '>', strtotime('2015-01-01'))->filterBy('date', '<', strtotime('2015-12-31'))

Not tested this, but i think this will work:

<?php

$today = new DateTime();
$future = $today->modify('+1 week');

$collection = page('blog')->children()->visible()->filterBy('date', '>', strtotime($today->format('Y-m-d')))->filterBy('date', '<', strtotime($future->format('Y-m-d'))) ?>

Then you can do what you like with $collection.

Edit: Actually thats not quite right… give me a minute…


#4

That is not quite logical, I think. You want to get everything that is newer than one week ago:

<?php
$today = new DateTime();
$oneWeekAgo = $today->modify('-1 week');
$collection = page('blog')->children()->visible()->filterBy('date', '>', $oneWeekAgo->format('U'));

?>

Or rather the specific date plus 1 week.


#5

Yup… i noticed as you were posting, you beat me to the solution :slight_smile:


#6

Okay… thanks :slightly_smiling_face:

But, I’m making a website for a Cinema. The thing I want to do is to display the text ‘Premiere’ for one week and the remove it from the title of the movie.

In the panel I have a date field with the premiere date…

Hope you get me?


#7

Then you can use an if statement:

<?php

$premiere = new DateTime($page->premiere());
$oneWeekAfter = $premiere->modify('+1 week');

if(time() < $oneWeekAfter->format('U')) {
  echo 'Premiere';
}
?>

Where $page->premiere() is the field that contains the premiere date.


#8

I think this would be better because it will include the 7th day:

if(time() <= $oneWeekAfter->format('U')) {
  echo 'Premiere';
}

I found when i was messing with removing events that had happened, that if i didnt use <= rather then just <, the events where disappearing the day before the day I wanted them too.


#9

Well, it depends on when you want your 7 day period to end. If I want my premiere to last 7 day and disappear on the 8th, then < is more useful, but that depends on how you define your week, so there is no right or wrong.


#10

Sure, I was just putting it out there :slight_smile:


#11

Thanks, it works perfectly :slight_smile: