how to select php for a date search field

Asked

Viewed 26 times

0

I have a problem to make a Select, I need help, I am programming in php, to do searches in a database I need to do a search for dates. I have already managed to do the search field by specific date, but I need to do the search with interval between dates. (example: search from 20/05/2020 to 31/05/2020).

the select I made to search for a date was this: case 'search reduroamdata':

        global $linhadataa; 
        global $rsdataa; 

        $dataa = new dataa();

        $data = $_POST['data'];


        $dataa->pesquisareduroamdata("SELECT * FROM usuarios WHERE data LIKE '%$data%' && ssid = 'EDUROAM' ");
        $linhadataa = $dataa->Linha;
        $rsdataa = $dataa->Result;

if anyone knows help me.

  • In your database the field data is a string type?

1 answer

2

Considering your example, there are many problems in your code beyond the query. But focusing on your question and considering that column data is of the DATE type, there are two simple ways to do this. The first is by using, in the case of Mysql, the clause BETWEEN, thus:

"SELECT * FROM usuarios WHERE data `BETWEEN` '$data1' AND '$data2' "

And the second way is to compare values:

"SELECT * FROM usuarios WHERE data >= '$data1' AND data <= '$data2' "

In case the column data be the text type, although not recommended, still you can perform the query using the function STR_TO_DATE. Details at that link

Browser other questions tagged

You are not signed in. Login or sign up in order to post.