Check if event is present

Hi,
I have two dates fields datedebut and datefin and I have to check if this event is still avaible today.
I was sure to get the correct comparison, but some “present” events are not viewed like “present”, and some “present” events would not have to be “present”…this is my code :

<?php  $expire  = $subpage->date(null, 'datefin');?>
<?php  $today = date('Y-m-d');?>
<?php  $today_time = strtotime($today); ?>
<?php  $expire_time = strtotime($expire); ?>
<?php  if($expire_time > $today_time): ?><br><span class="still">still visible</span><?php endif ?>`

In my config file, I have these lines :

c::set('locale', 'fr_FR.ISO8859-15');
c::set('date.handler', 'strftime');
c::set('date.timezone', 'Europe/Paris');

I don’t know if I do it correctly, or where I’m wrong…

If you set your date handler to strftime, you have to use date. formats that work. with strtime, not Y-m-d

https://secure.php.net/manual/de/function.strftime.php

Just on a side note: The above already returns a timestamp, no need to convert it again three lines later.

Okay, thanks, so I simplified like this

<?php  $expire  = strftime('%d.%m.%y', $subpage->date(null, 'datefin'));?>
<?php  $today = strftime('%d.%m.%Y');?>
<?php  if($expire > $today): ?><br> <span class="still">still visible</span><?php endif ?>

But doesn’t return things as expected. Some events can be displayed with future dates and I have to be carefull to not mention them as “present” too…

Well, you are only checking the end date, not the start date… So you should actually check if the start date is prior or equal to today and. the end date still in the future. While future events have a start date and end date in the future.

Makes sense, of course. Tried to translate it as best as possible, I did that, but this is still the mess in my head

<?php  $expire  = strftime('%d.%m.%y', $subpage->date(null, 'datefin'));?>
<?php  $begin  = strftime('%d.%m.%y', $subpage->date(null, 'datedebut'));?>
<?php  $today = strftime('%d.%m.%Y'); ?>
<?php  if($begin <= $today || $today >= $expire): ?><br> <span class="still">still visible</span> <?php endif ?>

Should be && not ||-

Okay, I tried different things, but the most logical thing must be this

<?php if($begin <= $today && $expire >= $today): ?>
still not working :sleepy:

You should not use strftime here. Currently you compare the strings with %d.%m.%y that does not work here. For example: '20.10.2018' < '21.09.2018' will be true.

You should simply use the unix timestamp to compare the dates.
There was also an error with … && $today >= $expire should be … && $today <= $expire.

<?php

$expire = $subpage->date(null, 'datefin');
$begin = $subpage->date(null, 'datedebut');
$today = time();

?>

<?php if($begin <= $today && $today <= $expire): ?>
  <br> <span class="still">still visible</span>
<?php endif ?>
1 Like

Yes ! Thank you luka ! Everything rides like a charm now. Thank for your explanations too

What would be an adequate Kirby 3 solution for this? I am trying to use the answer above but I only get a “Object of class Kirby\Cms\Field could not be converted to int” error…

In Kirby 3 you get the Unix timestamp with

$page->nameOfDateField()->toDate()

i.e. without passing a format as parameter.