How to query Pdo by date using input Month?

Asked

Viewed 187 times

0

inserir a descrição da imagem aqui

I would like to know how I can make an appointment for the month chosen along with the name. Like if I choose August it will only come month of August and so on and so forth.

This is the query

public function RetornaAtualizacoesFuncionarios($data,$codusuario){ 
        $WHERE = array();
        if( empty( $codusuario    ) ) {$WHERE[] = "codusuario = :codusuario";}else{ $WHERE[] = "codusuario != '37'"; }
        ;
        if( empty( $data   ) ) $WHERE[] = "DATE_FORMAT(data,'%Y-%m') = :data";          
    try {
         $Query = "SELECT 
        DISTINCT(usuario),
                date_format(data,'%d/%m/%Y %H:%i:%s') as data,
                codigo,
                nome 
                    FROM funcionarios
                         WHERE codusuario = '$codusuario'
                             ORDER BY data DESC  ";
        if( !empty($WHERE) ) $this->$Query .= ' WHERE '.implode(' AND ', $WHERE );

echo "<pre>"; print_r($_POST); 
echo "<pre>"; print_r($Query);exit;
        include_once $_SESSION['pmodel'].'/classes/mysqlconnection_class.php';
               $p_sql = MysqlConnection::getInstance()->prepare($Query);

                $_retorno = array(); 

                if($p_sql->execute()) {
                    while ($_result = $p_sql->fetch(PDO::FETCH_ASSOC))  {
                        $_retorno[] = $_result; 
                    }
                }

                return $_retorno;
            } catch (PDOException $e) {
                echo $e->getMessage();
            }
        }
}

this is the input

<form class="form"  id="exibefuncionarios" method="post" target='_blank' action="gerar">
        <fieldset>
            <div style="border-radius:5px;padding:10px;">
                <table class="tbl_relatorio" cellspacing='0' width='100%' border ='0'>
                    <tr>
                        <td class='label_1'>Período:</td> 
                        <td>
                             <input style="width: 290px;" name='data' type='month'/>
                            </select>
                        </td>
                    </tr>
                </table>
            </div>
        </fieldset>

1 answer

0

To be able to filter certain records within Mysql, you can do it as follows:

Filtering Records by the Day

SELECT * FROM tabela WHERE DAY(data) = 'dia_escolhido'

Filtering Records by Month

SELECT * FROM tabela WHERE MONTH(data) = 'mes_escolhido'

Filtering Records by the Year

SELECT * FROM tabela WHERE YEAR(data) = 'ano_escolhido'

Using Day, Month, Year - can filter the data correctly.

Browser other questions tagged

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