I am trying to do a sql with between. The date data is entered first as a string. Any help with this question?
CodePudding user response:
The string is dd/mm/yyyy
$var = '28/09/2021';
$date = str_replace('/', '-', $var);
$fecha_inicio=date('Y-m-d', strtotime($date));
//dd($fecha_inicio);
At the final you will get date like 2021-09-28 and now we can do a between with actual date 2021-10-01
select t from App\Entity\table1 t, App\Entity\table2 s where s.id=t.id and s.id=50 AND (s.fecha_solicitud BETWEEN ('2021-09-28') AND ('2021-10-01') ) order by s.field1 asc, s.field2 asc, s.field3 asc
after this you can see the sentence in DB with dd($query->getSQL()); function
SELECT
a0_.*,
FROM
tabla1 a0_,
tabla2 s1_
WHERE
s1_.id = a0_.id
AND s1_.id = 23
AND (
s1_.fecha_solicitud BETWEEN ('2021-09-28')
AND ('2021-10-01')
)
ORDER BY
s1_.field1 ASC,
s1_.field2 ASC,
s1_.field3 ASC