Skip to content

Instantly share code, notes, and snippets.

@ahoulgrave
Created December 1, 2015 17:43
Show Gist options
  • Save ahoulgrave/42bd4a0c6a93a98e5349 to your computer and use it in GitHub Desktop.
Save ahoulgrave/42bd4a0c6a93a98e5349 to your computer and use it in GitHub Desktop.
Tiempo transcurrido en español
function time_elapsed_string($datetime, $full = false) {
$now = new \DateTime;
$ago = $datetime;
$diff = (array) $now->diff($ago);
$diff['w'] = floor($diff['d'] / 7);
$diff['d'] -= $diff['w'] * 7;
$string = array(
'y' => 'año',
'm' => 'mes',
'w' => 'semana',
'd' => 'dia',
'h' => 'hora',
'i' => 'minuto',
's' => 'segundo'
);
foreach ($string as $k => &$v) {
if ($diff[$k]) {
$v = $diff[$k] . ' ' . $v . ($diff[$k] > 1 ? (($k == 'm')?'es':'s') : '');
} else {
unset($string[$k]);
}
}
if (!$full) $string = array_slice($string, 0, 1);
return $string ? 'Hace ' . implode(', ', $string): 'Ahora';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment