Ver Mensaje Individual
  #3 (permalink)  
Antiguo 09/11/2011, 17:26
Avatar de gnzsoloyo
gnzsoloyo
Moderador criollo
 
Fecha de Ingreso: noviembre-2007
Ubicación: Actualmente en Buenos Aires (el enemigo ancestral)
Mensajes: 23.324
Antigüedad: 16 años, 5 meses
Puntos: 2658
Respuesta: [APORTE] Calcular tiempo transcurrido entre dos fechas, obtener X AÑOS X M

Trata de no complicar tanto los cálculos. Puedes tomar esto como base; lo hice hace algunos años para un desarrollo y todavía está en uso.
El objetivo era establecer cuánto tiempo había pasado entre dos fechas, tomando la diferencia en segundos entre ambas.
Código MySQL:
Ver original
  1. DELIMITER $$
  2.  
  3. DROP FUNCTION IF EXISTS `FN_DEVOLVERTIEMPO` $$
  4. CREATE FUNCTION `FN_DEVOLVERTIEMPO`(TOTALSEG INT) RETURNS char(32) CHARSET latin1
  5.     DECLARE TIEMPO CHAR(32);
  6.     DECLARE DIA INT DEFAULT 0;
  7.     DECLARE HORA INT DEFAULT 0;
  8.     DECLARE MINUTO INT DEFAULT 0;
  9.     DECLARE SEGUNDO INT DEFAULT 0;
  10.     SET DIA= TOTALSEG DIV 86400;
  11.     SET TOTALSEG = TOTALSEG % 86400;
  12.     SET HORA= TOTALSEG DIV 3600;
  13.     SET TOTALSEG = TOTALSEG % 3600;
  14.     SET MINUTO = TOTALSEG DIV 60;
  15.     SET SEGUNDO= TOTALSEG % 60;
  16.     SET TIEMPO=CONCAT(DIA, ' D. ', HORA, ' hs. ', MINUTO,' min. ', SEGUNDO,' seg.');
  17.     RETURN TIEMPO;
  18. END $$
  19.  
  20. DELIMITER ;
Te dejo esta base para que tu resuelvas la parte de años y meses.
__________________
¿A quién le enseñan sus aciertos?, si yo aprendo de mis errores constantemente...
"El problema es la interfase silla-teclado." (Gillermo Luque)