Skip to content

Instantly share code, notes, and snippets.

@andersonfraga
Created July 16, 2014 12:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andersonfraga/04b9744f1f6e3a55e0dd to your computer and use it in GitHub Desktop.
Save andersonfraga/04b9744f1f6e3a55e0dd to your computer and use it in GitHub Desktop.
Tempo retroativo por extenso pt-br
<?php
function dateFormat($date) {
if (!$date) {
return '';
}
$nowDay = new DateTime('now');
$pastDay = new DateTime($date);
$interval = $nowDay->diff($pastDay);
$mapExtended = array(
'y' => array('ano', 'anos'),
'm' => array('mês', 'meses'),
'd' => array('dia', 'dias'),
'h' => array('hora', 'horas'),
'i' => array('minuto', 'minutos'),
's' => array('segundo', 'segundos'),
);
$message = array();
foreach ($mapExtended as $key => $map) {
if ($interval->$key > 0 && count($message) < 2) {
$message[] = $interval->$key . ' ' . $map[$interval->$key == 1 ? 0 : 1];
}
}
return 'há ' . implode(' e ', $message);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment