Home > Software engineering >  How to hide events that happened today and have passed ? Symfony
How to hide events that happened today and have passed ? Symfony

Time:10-13

In my Symfony project, I have created the table "event" and datatime field in it named start. In twig, I wish to filter and display upcoming events. So events that have passed would be visible any more.

At the moment, I used {% if event.start > date() %}. It worked to hide events that happened days before today. I wanted also to hide events that already happened today but currently it doesn't work when time has passed of the today's event.

How can I hide events that time already has passed ?

CodePudding user response:

better to make a function in the repository and call it in the controller not using findAll and hide events.

// EventRepository
public function eventsList()
{
    return $this->createQueryBuilder('e')
        ->andWhere('e.date >= :today')
        ->setParameter('today', new \DateTime())
        ->orderBy('e.id', 'DESC')
        ->getQuery()
        ->getResult()
    ;
}

CodePudding user response:

Here is the solution :

  • Make sure the field is datetime.

  • Use {% if event.start > 'now' %}and it will take into account day (of today) and (current) time.

  • Related