Ver Mensaje Individual
  #1 (permalink)  
Antiguo 03/10/2010, 02:36
Avatar de neodani
neodani
 
Fecha de Ingreso: marzo-2007
Mensajes: 1.811
Antigüedad: 17 años, 2 meses
Puntos: 20
[APORTE] Calcular edad

Buenas,

He encontrado diferentes métodos para encontrar la edad de un usuario.

Ejemplo1
Código PHP:
Ver original
  1. function Edad($fecha_nacimiento){
  2.     //fecha actual
  3.     $dia=date(j);
  4.     $mes=date(n);
  5.     $ano=date(Y);
  6.      
  7.     //fecha de nacimiento
  8.     $dianaz=12;
  9.     $mesnaz=10;
  10.     $anonaz=1985;
  11.      
  12.     //si el mes es el mismo pero el día inferior aun no ha cumplido años, le quitaremos un año al actual
  13.     if (($mesnaz == $mes) && ($dianaz > $dia)) {
  14.     $ano=($ano-1); }
  15.      
  16.     //si el mes es superior al actual tampoco habrá cumplido años, por eso le quitamos un año al actual
  17.     if ($mesnaz > $mes) {
  18.     $ano=($ano-1);}
  19.      
  20.     //ya no habría mas condiciones, ahora simplemente restamos los años y mostramos el resultado como su edad
  21.     $edad=($ano-$anonaz);
  22.     return $edad;
  23. }
  24.  
  25. // Ejecutamos la función
  26. $fecha_nacimiento = "2009/1/1";
  27. echo strtotime($fecha_nacimiento);


Ejemplo2
Código PHP:
Ver original
  1. function determine_age($birth_date)
  2. {
  3. $birth_date_time = strtotime($birth_date);
  4. $to_date = date('m/d/Y', $birth_date_time);
  5.  
  6. list($birth_month, $birth_day, $birth_year) = explode('/', $to_date);
  7.  
  8. $now = time();
  9.  
  10. $current_year = date("Y");
  11.  
  12. $this_year_birth_date = $birth_month.'/'.$birth_day.'/'.$current_year;
  13. $this_year_birth_date_timestamp = strtotime($this_year_birth_date);
  14.  
  15. $years_old = $current_year - $birth_year;
  16.  
  17. if($now < $this_year_birth_date_timestamp)
  18. {
  19. /* his/her birthday hasn't yet arrived this year */
  20. $years_old = $years_old - 1;
  21. }
  22.  
  23. return $years_old;
  24. }
  25.  
  26. // You can write about any English textual datetime description
  27.  
  28. $birth_date = '6 Feb 1985';
  29.  
  30. $age = determine_age($birth_date);
  31.  
  32. echo $age;

Pero sin duda, que os parece el siguiente ejemplo número 3.

Ejemplo3
Código PHP:
Ver original
  1. function get_age($dob)
  2. {
  3.     $t = time();
  4.     $age = ($dob < 0) ? ( $t   ($dob * -1) ) : $t - $dob;
  5.     return floor($age / 31536000); // 60 * 60 * 24 * 365;
  6. }
  7. echo "Edad: ".get_age('504057600');

Hace lo mismo que el resto pero en 3 lineas, me dejo algo¿?

Saludos!