Created
December 1, 2015 17:43
-
-
Save ahoulgrave/42bd4a0c6a93a98e5349 to your computer and use it in GitHub Desktop.
Tiempo transcurrido en español
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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