Ways to take only the month of the dates recorded in the database

Asked

Viewed 202 times

0

I need to take only the months of the dates of a given field that is in my database, however, preferably without using SELECT MONTH.

That way, we get the current month of today’s date:

$mes_atual = date('m'); 
echo $mes_atual; //Seria exibido "06" - hoje é dia 22/06/2020

I was looking for something similar, but with the bank dates, I’m going to leave an example that I know doesn’t make much sense, but just so you understand better.

My example code:

   $sql = "SELECT prazo FROM comercio";
   $resultado = mysqli_query($con, $sql);

   while ($row= mysqli_fetch_array($resultado)) {
  
   $mes_atual = date('m')$row['prazo'];  //Aqui pegaria somente o mês das datas vindas do banco

   }

Is it possible I rescue these months from database otherwise? without being using the SELECT MONTH, if NAY, why?

  • Either you recover the full date of your DBMS and extract the month in your application or just recover the month using the function month, from your DBMS and delivers only the month to your application. I couldn’t understand your final question. Obviously the less data you traffic on the network the more efficient your application will be.

  • @anonymous in my post, basically I’m asking if there are other ways to rescue only the month of a date that is in the database (Mysql), without using the SELECT MONTH

  • What’s wrong with "Month" ?

  • SELECT term FROM trade Where Month(now()) = Month(term) would be this ?

  • No, it is displayed the current month and then the bank date... This example code is to say that I want to take only the month of this bank date. I’m managing to solve the problem and I’ll post the answer here, but I had nowhere to run...

  • Explains but doesn’t make it complicated. First you say //It would be displayed "06" - today is day 22/06/2020 -- then you say //Here would take only the month of the dates coming from the bank. So it’s today or all the dates?

  • I understood a thing.

  • @Leocaracciolo of all the dates they have in the field of the bank, sorry if I was unclear, but the POST was solved.

Show 3 more comments

3 answers

1


$sql = ("SELECT prazo FROM comercio");

$resultado = mysqli_query($con, $sql);

if (mysqli_num_rows($resultado) > 0) {

    while($row = mysqli_fetch_array($resultado)) {
    
        //retorna um novo objeto DateTime.
        $date = date_create($row['prazo']);
        
        //retorna uma data formatada de acordo com o formato especificado
        $mes_atual =  date_format($date, 'm');
        $data = date_format($date, 'd/m/Y');
        echo $mes_atual." -  Prazo ". $data;
        echo "<br>\n";
        
    }   
    
}
  • Very well, I did it another way, but yours worked too. I’ll be responding with my answer, but I’ll leave yours as a solution, grateful.

0

I had nowhere to run, and I ended up using the SELECT MONTH, I didn’t know I could select a field twice in one SELECT, follows my answer below that it worked:

$sql = "SELECT comercio.*, date_format(comercio.prazo,'%m') as mes_prazo, tabela1.nome, tabela2.preco
FROM comercio, tabela1, tabela2
WHERE comercio.nome = tabela1.nome";

$resultado = mysqli_query($con, $sql);

while ($cont = mysqli_fetch_array($resultado)) {

echo $cont['mes_prazo']; //Aqui exibe todos os meses registrados no banco, que eu precisava usar fazer uma condição. Além desses meses são exibidos as datas completas...

}
  • Hahaha, here you are going against your question "and preference without using SELECT MONTH". But it’s worth, the important thing is to be happy!!! + 1

  • Yes kkkkk, why would I have to work with 2 while and would not like to, but the important thing is that it worked!

0

  • I don’t know if that helps me...

  • But that’s another way to return the month, wouldn’t it? What is your difficulty?

  • Wouldn’t be that, your answer returns the current month at SQL, would need ways to return the months of bank dates in the PHP... I tried to use this example in PHP also (for bank dates), but without success.

  • Just change where you are now() and put the field you want that returns the month. SELECT date_format(term,'%m') FROM trade

  • Thank you for trying to help Rodrigo!

Browser other questions tagged

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