skip to Main Content

This is what I tried:

    $time = date('Y/m/d');
    $alerts = $repository->findBy(['datum' => $time]);

and

    $time = date('Y/m/d');
    $lessons = $repository->createQueryBuilder('q')
        ->where('q.datum LIKE currentDate')
        ->setParameter('currentDate', $time);

enter image description here

For some reason those don’t work and I have no idea why. It should be displaying the last one in a dd($lessons), but it doesn’t

Update:

This is what I got rn, but it’s still not working:

    $today = new DateTime('today');
    $tomorrow = new DateTime('today +1 day');

    $lessons = $repository->createQueryBuilder('q')
        ->where('q.datum > :today')
        ->andWhere('q.datum < :tomorrow')
        ->setParameter('today', $today)
        ->setParameter('tomorrow', $tomorrow);

3

Answers


  1. Why don’t you use the current database date function to perform this query? It would look like this:

    $lessons = $repository->createQueryBuilder('q')
            ->where('q.datum > now()')
            ->andWhere('q.datum < now()+ INTERVAL 1 DAY');
    
    Login or Signup to reply.
  2. Your checking the date format ‘Y/m/d’ (2023/05/11) with a saved date field format in the database ‘Y-m-d’ (2023-05-11)

    Besides that your checking if you can find the current date but according to your database table there is no current date.

    The other check that your doing is checking if there is a date higher then 2023-05-11 00:00:00, you should change this to higher and the same ‘>=’

    Login or Signup to reply.
  3. i guess your entity date field is a datetime field (or at least a date field), when not, make sure it is, then you need to use the DateTime Object for searching

    your entity field should look like this

    /**
     * @ORMColumn(type="date")
     */
    private $datum;
    
    # [...]
    
    public function getDatum(): ?DateTimeInterface
    {
        return $this->datum;
    }
    
    public function setDatum(DateTimeInterface $datum): self
    {
        $this->datum = $datum;
    
        return $this;
    }
    

    your repo function can be look like this

    // src/Repository/TableRepository.php
    public function findByStartAndEndDate($dateStart, $dateEnd)
    {
        $qb = $this->createQueryBuilder('q');
    
        return $qb
            ->andWhere(
                $qb->expr()->between('q.datum', ':dateStart', ':dateEnd')
            )
            ->setParameter('dateStart', $dateStart)
            ->setParameter('dateEnd', $dateEnd)
            ->getQuery()
            ->getResult()
        ;
    }
    

    using that funcion inside your controller

    // src/Controller/TableController.php
    $dateStart = new DateTime();
    $dateEnd = new DateTime('+1 days');
    
    $items = $repository->findByStartAndEndDate($dateStart, $dateEnd);
    

    gl hf

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search