Skip to content

Instantly share code, notes, and snippets.

@fastcodecoq
Last active August 29, 2015 14:03
Show Gist options
  • Select an option

  • Save fastcodecoq/f9b914ffbe928cb2929d to your computer and use it in GitHub Desktop.

Select an option

Save fastcodecoq/f9b914ffbe928cb2929d to your computer and use it in GitHub Desktop.
// String cuando_paso
// params
// int time <tiempo unix>
function cuando_paso (time)
{
var time = Math.floor( (new Date().getTime() / 1000) - Math.floor(time / 1000)); // obtenemos el tiempo actual
var tiempos = {
31536000 : 'año',
2592000 : 'mes',
604800 : 'semana',
86400 : 'día',
3600 : 'hora',
60 : 'minuto',
1 : 'segundo'
};
var postfijos = {
'año' : 's',
'mes' : 'es',
'semana' : 's',
'día' : 's',
'hora' : 's',
'minuto' : 's',
'segundo' : 's'
};
for (unidad in tiempos) {
if (time < Math.floor(unidad)){
var hubo = Math.floor(time / unidad);
return hubo + ' ' + tiempos[unidad] + ((hubo>1)? postfijos[tiempos[unidad]] : '' );
}
}
}
//llamado cuando_paso((new Date().getTime());
<?php
// String cuando_paso
// params
// int $time <tiempo unix>
function cuando_paso ($time)
{
$time = time() - $time; // obtenemos el tiempo actual
$tiempos = array (
31536000 => 'año',
2592000 => 'mes',
604800 => 'semana',
86400 => 'día',
3600 => 'hora',
60 => 'minuto',
1 => 'segudno'
);
$postfijos = array (
'año' => 's',
'mes' => 'es',
'semana' => 's',
'día' => 's',
'hora' => 's',
'minuto' => 's',
'segundo' => 's'
);
foreach ($tiempos as $unidad => $fracion) {
if ($time < $unidad) continue;
$hubo = floor($time / $unidad);
return $hubo.' '.$fracion.(($hubo>1)? $postfijos[$fracion] : '' );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment