Ir para conteúdo

POWERED BY:

Arquivado

Este tópico foi arquivado e está fechado para novas respostas.

Ramon César

Pegar idade apartir da data de nascimento

Recommended Posts

Preciso pegar a idade do paciente a partir da data de nascimento, estou mandando meu código para vocês me ajudarem.

<?php 

$dataini = $_POST["inicio"];
$datafim = $_POST["fim"];
$dataatual = date("d-m-Y"); 

$select = "SELECT * FROM agendamento, paciente WHERE 

data_exame BETWEEN '$dataini' AND '$datafim' AND
id_paciente = paciente_id_paciente
GROUP BY  data_exame ASC, horario_age ASC";

$exec = mysql_query($select, $conexao) or die(mysql_error());
$total = mysql_num_rows($exec);
echo "<p class=\"resto\"><b>Foi encontrado $total resultado(s).</b></p>"; 

echo "<div id=\"tabela\"><table width=\"100%\" border=\"1\">
<tr>
   <td><p class=\"paciente\"><b>Data:</b> </td>
<td><p class=\"paciente\"><b>Horário:</b> </td>
   <td><p class=\"paciente\"><b>Paciente:</b> </td>
   <td><p class=\"paciente\"><b>Tipo Exame:</b> </td>
 </tr>

 ";  

while($dados=mysql_fetch_array($exec)) { 
// exibindo os dados encontrados 

echo "
<tr>
   <td>".$idade . "</td>
<td>".$dados['horario_age']  . "</td>
   <td>".$dados['nome_pac']  ."</td>
   <td>".$dados['tipo_exame']  ."</td>

";
}

?>

Compartilhar este post


Link para o post
Compartilhar em outros sites

cara ja vi varios funções em php, mas to apnhanhndo para colocar em meu código. da para você dar um ezemplo de como eu posso inserir essa função no meu while, retornando a iddade do paciente

Compartilhar este post


Link para o post
Compartilhar em outros sites

<?php
   $ano_nascto = 1990;
   $ano_atual  = date("Y");
   $idade_paci = $ano_atual - $ano_nascto;
   echo $idade_paci;
   // Irá imprimir 22, que é a idade do paciente
?>

 

Passa estrutura da tua tabela, para vermos quais são os campos e adaptarmos os scripts!

Compartilhar este post


Link para o post
Compartilhar em outros sites

A estrutura das tabelas

 

CREATE TABLE IF NOT EXISTS `agendamento` (
 `id_agendamento` int(15) unsigned NOT NULL AUTO_INCREMENT,
 `paciente_id_paciente` int(15) NOT NULL,
 `sus_age` int(15) unsigned DEFAULT NULL,
 `nascimento` varchar(8) DEFAULT NULL,
 `tipo_exame` varchar(150) DEFAULT NULL,
 `data_exame` varchar(10) DEFAULT NULL,
 `horario_age` varchar(6) DEFAULT NULL,
 PRIMARY KEY (`id_agendamento`),
 KEY `agendamento_FKIndex1` (`paciente_id_paciente`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=11 ;

CREATE TABLE IF NOT EXISTS `paciente` (
 `id_paciente` int(15) NOT NULL AUTO_INCREMENT,
 `nome_pac` varchar(80) DEFAULT NULL,
 `sus_pac` int(11) NOT NULL,
 `endereco` varchar(60) DEFAULT NULL,
 `bairro_pac` varchar(20) DEFAULT NULL,
 `nascimento_pac` varchar(10) DEFAULT NULL,
 `telefone_pac` varchar(60) DEFAULT NULL,
 `status_pac` varchar(15) DEFAULT NULL,
 PRIMARY KEY (`id_paciente`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=10 ;

fiz um form para pesquisar agendamentos de paciente entre a data inial e final e com isso tragos os dados em um while, so que em vez da data de nascimento quero a idade do paciente.

Compartilhar este post


Link para o post
Compartilhar em outros sites

Quando você trouxer as informações do banco, a data virá no formato YYYY-MM-DD, então basta pegar os primeiros quatro caracteres da string.

 

Quanto a solução do Wanderson, tem apenas uma coisa que precisa ser levada em consideração: o mês, caso contrário, pode haver uma diferença de um ano.

 

E, claro, eu prefiro usar a classe DateTime do PHP para esse tipo de cálculo, e ainda evita você contornar esse problema com substr(), por exemplo:

 

<?php

$birthDate = new DateTime( '1988-03-13' );

$diff = $birthDate -> diff( new DateTime );

if( $diff -> y > 0 ) {

   printf( 'You have %d %s', $diff -> y, ( $diff -> y > 1 ? 'years' : 'year' ) );

} else {

   print 'You\'re just a baby. Why are using the computer?';
}

Compartilhar este post


Link para o post
Compartilhar em outros sites

Oi amigo, eu fiz um script aqui pra você, mas não deu pra eu testar, testa e me fala:

 

<?php
   function get_age ($nascto) {
       $age = date("Y") - $nascto;
return $age;
   }
   $dataini = mysql_real_escape_string($_POST['inicio']);
   $datafim = mysql_real_escape_string($_POST['fim']);
   $hoje    = date("d-m-Y"); // 00-00-0000

   $sql     = "SELECT * FROM `agendamento`, `paciente` WHERE `data_exame` ";
   $sql    .= "BETWEEN ".$dataini." AND ".$datafim." AND `id_paciente` = `paciente_id_paciente` ";
   $sql    .= "GROUP BY `data_exame` ASC, `horario_age` ASC";

   $query   = mysql_query($sql, $conexao) or die("MYSQL: ".mysql_error());
   $total   = mysql_num_rows($query);
   echo "<p class='resto'><b>Forma encontrados ".$total." resultado(s)</b></p>";

   if ($total > 0) {
?>
   <table width="100%" border="1">
   <tr>
       <td><p class="paciente"><b>Idade:</b></td>
       <td><p class="paciente"><b>Horário:</b></td>
       <td><p class="paciente"><b>Paciente:</b></td>
       <td><p class="paciente"><b>Tipo Exame:</b></td>
   </tr>
<?php
   while ($res = mysql_fetch_object($query)) {
?>
<div id="tabela">
   <tr>
       <td><?=get_age($res->nascimento_pac);?></td>
       <td><?=$res->horario_age?></td>
       <td><?=$res->nome_pac?></td>
       <td><?=$res->tipo_exame?></td>
   </tr>
</div>
<?php
   }
?>
</table>
<?php
   }
?>

Compartilhar este post


Link para o post
Compartilhar em outros sites

AMIGO DA UMA OLHADA AI NO QUE FIZ, NAO DA NEM UM ERRO SO QUE TMB NAO TO RECEBENDO A IDADE

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>HCAL - SERVIÇO DE TOMOGRAFIA</title>
<link href="../css.css" rel="stylesheet" type="text/css" media="screen" />
</head>

<body>
<div id="geral">
<div id="topo"><a href="form.php"><img
src="../img/imggovernoindex.png" border="none" /></a>
<h3 class="ptopo">SISTEMA ÚNICO DE SAÚDE DO ESTADO DO
AMAPÁ<br />
SECRETARIA DE ESTADO DA SAÚDE DO AMAPÁ - SESA<BR />
VERSÃO 1.0 - UNIDADE DE INFORMÁTICA - 3222- 1530<BR />
<a>Sair</a> </h3></div>
<?php include"agenda.php"; ?>
<div id="conteudo">
<?php
$conexao = mysql_connect("localhost", "root", "") or die(mysql_error());
$db = mysql_select_db("hcal");
?>
<?php

$dataini = $_POST["inicio"];
$datafim = $_POST["fim"];

$select = "SELECT * ,
(YEAR(CURDATE()) - YEAR(nascimento_pac)) - (RIGHT(CURDATE(),5) <
RIGHT(nascimento_pac,5)) AS idade
FROM paciente, agendamento WHERE
data_exame BETWEEN '$dataini' AND '$datafim' AND
id_paciente = paciente_id_paciente
GROUP BY  data_exame ASC, horario_age ASC";

$exec = mysql_query($select, $conexao) or die(mysql_error());
$total = mysql_num_rows($exec);
echo "<p class=\"resto\"><b>Foi encontrado $total resultado(s).</b></p>";

echo "<div id=\"tabela\"><table width=\"100%\" border=\"1\">
<tr>
   <td><p class=\"paciente\"><b>Data:</b> </td>
       <td><p class=\"paciente\"><b>Horário:</b> </td>
   <td><p class=\"paciente\"><b>Paciente:</b> </td>
       <td><p class=\"paciente\"><b>Idade:</b> </td>
   <td><p class=\"paciente\"><b>Tipo Exame:</b> </td>
 </tr>

 ";

while($dados=mysql_fetch_array($exec)) {

echo "
<tr>
   <td>".$dados['data_exame']  ."</td>
       <td>".$dados['horario_age']  . "</td>
   <td>".$dados['nome_pac']  ."</td>
       <td>".$dados['idade']  ."</td>
   <td>".$dados['tipo_exame']  ."</td>

";

}

?>
</tr>
 </table>
 </div>
</div>
</div>
</body>
</html>

CREATE TABLE IF NOT EXISTS `agendamento` (
 `id_agendamento` int(15) unsigned NOT NULL AUTO_INCREMENT,
 `paciente_id_paciente` int(15) NOT NULL,
 `sus_age` int(15) unsigned DEFAULT NULL,
 `nascimento` varchar(8) DEFAULT NULL,
 `tipo_exame` varchar(150) DEFAULT NULL,
 `data_exame` varchar(10) DEFAULT NULL,
 `horario_age` varchar(6) DEFAULT NULL,
 PRIMARY KEY (`id_agendamento`),
 KEY `agendamento_FKIndex1` (`paciente_id_paciente`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=11 ;


CREATE TABLE IF NOT EXISTS `paciente` (
 `id_paciente` int(15) NOT NULL AUTO_INCREMENT,
 `nome_pac` varchar(80) DEFAULT NULL,
 `sus_pac` int(11) NOT NULL,
 `endereco` varchar(60) DEFAULT NULL,
 `bairro_pac` varchar(20) DEFAULT NULL,
 `nascimento_pac` varchar(10) DEFAULT NULL,
 `telefone_pac` varchar(60) DEFAULT NULL,
 `status_pac` varchar(15) DEFAULT NULL,
 PRIMARY KEY (`id_paciente`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=10 ;

Compartilhar este post


Link para o post
Compartilhar em outros sites

×

Informação importante

Ao usar o fórum, você concorda com nossos Termos e condições.