View day selection with php and Pdo

Asked

Viewed 84 times

0

On this page I’m making birthday of the month, and I was able to display all the birthdays that have in the current month, but I’m not able to display the day and I also selected in the database...

                   <?php
                            require 'cfg/db.php';

                             $conexao = conexao::getInstance();
                             $sql = 'SELECT id, nome, membro, foto, data_nascimento, DAY(data_nascimento) FROM membros WHERE DAY(data_nascimento) AND MONTH(data_nascimento)';
                             $stm = $conexao->prepare($sql);
                             $stm->execute();
                             $aniversariantes = $stm->fetchAll(PDO::FETCH_OBJ);
                             print_r($aniversariantes);
                      ?>

                      <?php 
            
            foreach($aniversariantes as $aniversariante):

                      ?>

                      <tr class="table-row">
                            <td class="table-img">
                              <img src='fotos/<?=$aniversariante->foto?>' height='40' width='40'>
                            </td>
                            <td class="table-text">
                              <h6><?=$aniversariante->nome?></h6>
                                <p><?=$aniversariante->membro?></p>
                            </td>

                            <td class="march">
                               Dia: 
                            </td>
                        </tr>
            <?php 

            endforeach;
            
            ?>

1 answer

2


Change your select for:

SELECT id, nome, membro, foto, data_nascimento, DAY(data_nascimento) AS dia_nascimento FROM membros WHERE DAY(data_nascimento) AND MONTH(data_nascimento)

and at the time of printing you do so:

<td class="march">
    Dia: <? echo $aniversariante->dia_nascimento; ?>
</td>

The AS works as a nickname, so the column of name DAY(data_nascimento) was dubbed as dia_nascimento.

  • worked out, thanks a lot ! you could help me in one more thing?

  • what the doubt? If it is too extensive the ideal is to create a new question.

  • is that she is in yyyyyy/mm/dd wanted to put her in the format of Brazil

  • To format the date you use the function DATE_FORMAT, then it would look like this DATE_FORMAT(DATE(data_nascimento), "%d/%m/%Y") AS dia_nascimento in place of DAY(data_nascimento) AS dia_nascimento.

  • I did, thank you very much !

Browser other questions tagged

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