Created
July 16, 2014 12:10
-
-
Save andersonfraga/04b9744f1f6e3a55e0dd to your computer and use it in GitHub Desktop.
Tempo retroativo por extenso pt-br
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
<?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