Date filter using month

Asked

Viewed 29 times

0

Dear(a)s

The following is, I have a form where I do some filters/ searches that displays the results in a table, but I need to filter the dates by month that are part of the date itself, like month 01,month 02 etc, so far I did not succeed, someone can give me a north?

create a combobox where I spend the months, in the query I tried the following implementation AND MONTH (data_process) LIKE '$parametro_data%',

<form>
      <div class="col-sm-2" style="">
           <select name="parametro_data" id="data" class="form-control">
                        <option value="">MÊS</option>
                        <option value="12">12</option>
                        <option value="11">11</option>
                        <option value="10">10</option>
                        <option value="09">09</option>
                        <option value="08">08</option>
                        <option value="07">07</option>
                        <option value="06">06</option>
                        <option value="05">05</option>
                        <option value="04">04</option>
                        <option value="03">03</option>
                        <option value="02">02</option>
                        <option value="01">01</option>  
                    </select>
                </div>
        </form>


        <?php
        $parametro_data = filter_input(INPUT_GET, "parametro_data");
        
        $sql = "SELECT tb_processo.codigo_processo,tb_processo.data_processo 
        FROM 
        tb_processo
        WHERE MONTH (data_processo) LIKE '$parametro_data%');

however no result is returned

1 answer

0


The LIKE operator applies to text type fields. Since your field is date, and you want to filter only one month, try doing it as follows:

 $sql = "SELECT tb_processo.codigo_processo,tb_processo.data_processo 
        FROM 
        tb_processo
        WHERE MONTH (data_processo) = " . $parametro_data;

If you need to filter more than 1 month, you can do something like this:

 $sql = "SELECT tb_processo.codigo_processo,tb_processo.data_processo 
    FROM 
    tb_processo
    WHERE MONTH (data_processo) in (1,2,3)";
  • Thanks Bins, thanks for the force

Browser other questions tagged

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