Función para mostrar "hace X minutos"

  • Autor Autor davidrod
  • Fecha de inicio Fecha de inicio
D

davidrod

Delta
Verificación en dos pasos activada
¡Usuario con pocos negocios! ¡Utiliza siempre saldo de Forobeta!
¡Hola betas!

Me encontré esta función en GitHub, sencilla y rápida, espero les sea de utilidad.

PHP:
function tiempoAtras($timestamp, $days = false, $format = "M j, Y"){
 
  if (!is_numeric($timestamp)) {
    // It's not a time stamp, so try to convert it...
    $timestamp = strtotime($timestamp);
  }
 
  if (!is_numeric($timestamp)) {
    // If its still not numeric, the format is not valid
    return false;
  }
 
  // Calculate the difference in seconds
  $difference = time() - $timestamp;
 
  // Check if we only want to calculate based on the day
  if ($days && $difference < (60*60*24)) {
    return "Hoy";
  }
  if ($difference < 3) {
    return "Ahora mismo";
  }
  if ($difference < 60) {   
    return "hace ".$difference . " segundos";
  }
  if ($difference < (60*2)) {   
    return "hace 1 minuto";
  }
  if ($difference < (60*60)) {
    return "hace ".intval($difference / 60) . " minutos";
  }
  if ($difference < (60*60*2)) {
    return "hace 1 hora";
  }
  if ($difference < (60*60*24)) {   
    return "hace ".intval($difference / (60*60)) . " horas";
  }
  if ($difference < (60*60*24*2)) {
    return "hace 1 d&iacute;a";
  }
  if ($difference < (60*60*24*7)) {
    return "hace ".intval($difference / (60*60*24)) . " d&iacute;as";
  }
  if ($difference < (60*60*24*7*2)) {
    return "hace 1 semana";
  }
  if ($difference < (60*60*24*7*(52/12))) {
    return "hace ".intval($difference / (60*60*24*7)) . " semanas";
  }
  if ($difference < (60*60*24*7*(52/12)*2)) {
    return "hace 1 mes";
  }
  if ($difference < (60*60*24*364)) {
    return "hace ".intval($difference / (60*60*24*7*(52/12))) . " meses";
  }
 
  // More than a year ago, just return the formatted date
  return @date($format, $timestamp);
}
 
Atrás
Arriba