Skip to content

Instantly share code, notes, and snippets.

@joelmiguelvalente
Created July 26, 2021 20:04
Show Gist options
  • Save joelmiguelvalente/1a4f51e683099b8557779bb6fbc41323 to your computer and use it in GitHub Desktop.
Save joelmiguelvalente/1a4f51e683099b8557779bb6fbc41323 to your computer and use it in GitHub Desktop.
<?php
/**
* Smarty plugin
* @package Smarty
* @subpackage plugins
*/
/**
* Smarty cat modifier plugin
*
* Type: modifier<br>
* Name: hace<br>
* Date: Feb 24, 2010
* Purpose: catenate a value to a variable
* Input: string to catenate
* Example: {$var|cat:"foo"}
* @link http://smarty.php.net/manual/en/language.modifier.cat.php cat
* (Smarty online manual)
* @author Ivan Molina Pavana
* @version 1.0
* @param string
* @param string
* @return string
*/
function smarty_modifier_hace($fecha, $show = false){
$ahora = time();
$tiempo = $ahora - $fecha;
//
$dias = round($tiempo / 86400);
// HOY
if($dias <= 0) {
// HACE MENOS DE 1 HORA
if(round($tiempo / 3600) <= 0){
// HACE MENOS DE 1 MINUTO
if(round($tiempo / 60) <= 0){
if($tiempo <= 60){ $hace = "Hace unos segundos"; }
// HACE X MINUTOS
} else {
$can = round($tiempo / 60);
$word = ($can <= 1) ? "minuto" : "minutos";
$hace = 'Hace '.$can. " ".$word;
}
// HACE X HORAS
} else {
$can = round($tiempo / 3600);
$word = ($can <= 1) ? "hora" : "horas";
$hace = 'Hace '.$can. " ".$word;
}
}
// MENOS DE 7 DIAS
else if($dias <= 30){
// AYER
$hace = ($dias < 2) ? 'Ayer' : 'Hace '.$dias.' d&iacute;as';
// HACE MAS DE UNA SEMANA
} else{
$meses = round($tiempo / 2592000);
if($meses == 1) $hace = 'M&aacute;s de 1 mes';
elseif($meses < 12) {
$hace = 'M&aacute;s de '.$meses.' meses';
} else {
$anos = round($tiempo / 31536000);
$hace = ($anos == 1) ? 'M&aacute;s de un a&ntilde;o' : 'M&aacute;s de '.$anos.' a&ntilde;os';
}
}
//
return $hace;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment