Jump to content
Fórum Script Brasil
  • 0

Cálculo com datas não funciona.


Edilson Santiago

Question

Olá, amigos. Pergunta de principiante, mas to quebrando a cabeça. Preciso listar todas os registros cuja idade hoje seja menor que 9 anos. 

Fiz assim: 

<?php 
$dataatual = date('Y-m-d');
$menos9anos= date('Y-m-d',strtotime('-3286 days'));
$criancas = "SELECT * FROM CadPessoas WHERE DtNasc BETWEEN $menos9anos AND $dataatual";
?>

<html>
  <head></head>
  <body>
  <table> 
        <thead>
            <tr bgcolor=#0404b8>
            <td align=center ><b>ID </b></td> 
            <td align=center ><b>NOME</b></td> 
            <td align=center ><b>CELULAR</b></td> 
            <td align=center ><b>DT.NASC.</b></td> 
            <td align=center ><b>STATUS</b></td> 
            <td align=center ><b>AÇÃO</b></td>
            </tr> 
        </thead>
        <tbody bgcolor=#3232e3>
  		<?php
          $dsn = ("mysql:host=localhost;dbname=meubanco;charset=utf8");
                $user = "meuusuario";
                $pass = "minhasenha";
                $pdo = new PDO($dsn,$user,$pass);
                $stm = $pdo->query($criancas);
                $rows = $stm->fetchAll();
                $count = count($rows);
                ?>
                <div style="color:#ffffff;  font-size:20px;" align="right" text-color="red"><b>Total de registros encontrados: <?php echo $count;?>&nbsp;&nbsp;</b></div>
                <?php
                foreach($rows as $row){
                    echo "<tr>";
                    echo ("<td align=center>{$row['Cod']}</td>");
                    echo ("<td>{$row['Nome']}</td>");
                    echo ("<td>{$row['Cel']}</td>");
                ?>
                    <td align=center><?php echo ( date('d/m/Y', strtotime($row['DtNasc'])) ); ?></td> 
                    <td align=center><?php echo $row['Status']; ?></td>
                    <td align=center><a href="pessoa_completo.php?<?php echo $row['Cod']?>"><img src="../img/more.png" alt="Imagem" height="39" width="100" />
                    </td> 
                <?php } ?>
        </tbody>
        
     </table><br>
    </body>
</html>

Mas ele está retornando pessoas com idade maior, por exemplo, nascidas em 1986. Deveria retornar só de 2013 pra cá. 

Detalhe: todas as datas do campo DtNasc do banco de dados estão salvas no formato 1998-12-04 00:00:00.

Abraços

Link to comment
Share on other sites

2 answers to this question

Recommended Posts

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



  • Forum Statistics

    • Total Topics
      152.1k
    • Total Posts
      652k
×
×
  • Create New...