Skip to content

Instantly share code, notes, and snippets.

@brunoarmanelli
Created September 1, 2017 13:12
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brunoarmanelli/18fe735cd11c89526fb570c4e58095ad to your computer and use it in GitHub Desktop.
Save brunoarmanelli/18fe735cd11c89526fb570c4e58095ad to your computer and use it in GitHub Desktop.
Tempo Relativo em PHP
<?php
function temporelativo($ts) {
if(!ctype_digit($ts)) {
$ts = strtotime($ts);
}
$diff = time() - $ts;
if($diff == 0) {
return 'agora';
} elseif($diff > 0) {
$day_diff = floor($diff / 86400);
if($day_diff == 0) {
if($diff < 60) return 'há poucos segundos';
if($diff < 120) return 'há 1 minuto';
if($diff < 3600) return 'há ' . floor($diff / 60) . ' minutos';
if($diff < 7200) return 'na última hora';
if($diff < 86400) return 'há ' . floor($diff / 3600) . ' horas';
}
if($day_diff == 1) { return 'há um dia'; }
if($day_diff < 7) { return 'há ' . $day_diff . ' dias'; }
if($day_diff < 31) { return 'há ' . ceil($day_diff / 7) . ' semanas'; }
if($day_diff < 60) { return 'há um mês'; }
return date('F Y', $ts);
} else {
$diff = abs($diff);
$day_diff = floor($diff / 86400);
if($day_diff == 0) {
if($diff < 120) { return 'em um minuto'; }
if($diff < 3600) { return 'em ' . floor($diff / 60) . ' minutos'; }
if($diff < 7200) { return 'em uma hora'; }
if($diff < 86400) { return 'em ' . floor($diff / 3600) . ' horas'; }
}
if($day_diff == 1) { return 'amanhã'; }
if($day_diff < 4) { return date('l', $ts); }
if($day_diff < 7 + (7 - date('w'))) { return 'na próxima semana'; }
if(ceil($day_diff / 7) < 4) { return 'em ' . ceil($day_diff / 7) . ' semanas'; }
if(date('n', $ts) == date('n') + 1) { return 'no próximo mês'; }
return date('F Y', $ts);
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment