calcular edad, solo falla por un dia PHP

guner

fecha de nacimiento ($fncido): 29/2/2003
fecha de hoy (mirar la fecha del post):
Código:

<?php

function&nbsp; cedad($fncido)
{
&nbsp; &nbsp; $fdhoy&nbsp; =&nbsp; explode("@",&nbsp; date('d@m@Y'));
&nbsp; &nbsp; $fpncido&nbsp; =&nbsp; explode('/',&nbsp; $fncido);
&nbsp; &nbsp; if($fncido[2]&nbsp; <&nbsp; $fdhoy[2])
&nbsp; &nbsp; {
&nbsp; &nbsp; &nbsp; &nbsp; if($fncido[1]&nbsp; <=&nbsp; $fdhoy[1])
&nbsp; &nbsp; &nbsp; &nbsp; {
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if($fncido[0]&nbsp; <=&nbsp; $fdhoy[0])
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $edad&nbsp; =&nbsp; $fdhoy[2]-$fpncido[2];
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $edad&nbsp; =&nbsp; $fdhoy[2]-$fpncido[2]-1;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }
&nbsp; &nbsp; &nbsp; &nbsp; }
&nbsp; &nbsp; &nbsp; &nbsp; else
&nbsp; &nbsp; &nbsp; &nbsp; {
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $edad&nbsp; =&nbsp; $fdhoy[2]-$fpncido[2]-1;
&nbsp; &nbsp; &nbsp; &nbsp; }
&nbsp; &nbsp; }
&nbsp; &nbsp; elseif($fncido[2]&nbsp; ==&nbsp; $fdhoy[2])
&nbsp; &nbsp; {
&nbsp; &nbsp; $edad&nbsp; =&nbsp; "0";
&nbsp; &nbsp; }else{
&nbsp; &nbsp; &nbsp; $edad&nbsp; =&nbsp; $fdhoy[2]&nbsp; -&nbsp; $fpncido[2];
&nbsp; &nbsp; }
}

cedad("29/2/2003")

?>
Se supone, que, al ser el mismo di del mismo mes, solo tiene que restar los años, y daria 1, pero no, parece que resta los años, y luego -1 ... si habeis visto el codigo sabeis que quiero decir

Soltrac

Haz una cosa..borra todo el codigo menos ese apartado y haber si realmente entra ahi o coge otro de los else...mas facil en las otras partes pon q sake algo por pantalla y haber en q rama del if entra al escribir eso.

guner

weno... ya está el código correcto:

<?php

function cedad($fncido)
{

&nbsp; &nbsp; &nbsp;$fdhoy = explode("@", date('d@m@Y'));
&nbsp; &nbsp; &nbsp;$fpncido = explode('/', $fncido);

&nbsp; &nbsp; &nbsp;if($fdhoy[1] == $fpncido[1])
&nbsp; &nbsp; &nbsp;{
&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp;if($fdhoy[0] >= $fpncido[0])
&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp;{
&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp;$edad = $fdhoy[2] - $fpncido[2];
&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp;}else{
&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp;$edad = $fdhoy[2] - $fpncido[2] - 1;
&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp;}
&nbsp; &nbsp; &nbsp;}elseif($fdhoy[1] <= $fpncido[1])
&nbsp; &nbsp; &nbsp;{
&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp;$edad = $fdhoy[2] - $fpncido[2] - 1;
&nbsp; &nbsp; &nbsp;}elseif($fdhoy[1] > $fpncido[1])
&nbsp; &nbsp; &nbsp;{
&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp;$edad = $fdhoy[2] - $fpncido[2];
&nbsp; &nbsp; &nbsp;}

&nbsp; &nbsp; &nbsp;echo $edad.' años';
}

cedad("29/03/2003");

?>

Usuarios habituales

  • guner
  • Soltrac