Last active
October 18, 2025 06:48
-
-
Save jeankassio/b567e36dabdcd92836d54a2a926ce49d to your computer and use it in GitHub Desktop.
Obter a distância de tempo em relação a uma data. Exemplos: "Há 1 hora", "Há 2 dias", "Quinta-Feira", etc
This file contains hidden or 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 | |
| public function toNotification($dataNotificacao){ | |
| $dataAtual = new DateTime(); | |
| $dataNotificacao = DateTime::createFromFormat('Y-m-d H:i:s', $dataNotificacao); | |
| if (!$dataNotificacao) { | |
| return false; | |
| } | |
| $intervalo = $dataAtual->diff($dataNotificacao); | |
| if($dataAtual->format('Y-m-d') === $dataNotificacao->format('Y-m-d')){ | |
| if ($intervalo->i < 1) { | |
| return "Há " . $intervalo->s . " segundo" . ($intervalo->s != 1 ? "s" : ""); | |
| } elseif ($intervalo->h < 1) { | |
| return "Há " . $intervalo->i . " minuto" . ($intervalo->i != 1 ? "s" : ""); | |
| } else { | |
| return "Há " . $intervalo->h . " hora" . ($intervalo->h != 1 ? "s" : ""); | |
| } | |
| }else{ | |
| $diasDiferenca = (int)$intervalo->format('%a'); | |
| if(date("Y-m-d", strtotime("-1 day")) == $dataNotificacao->format('Y-m-d')){ | |
| return "Ontem, às " . $dataNotificacao->format('H:i'); | |
| }elseif($diasDiferenca > 0 && $diasDiferenca <= 5){ | |
| $diasDaSemana = ['Domingo', 'Segunda-feira', 'Terça-feira', 'Quarta-feira', 'Quinta-feira', 'Sexta-feira', 'Sábado']; | |
| $diaSemana = $diasDaSemana[$dataNotificacao->format('w')]; | |
| return "$diaSemana às " . $dataNotificacao->format('H:i'); | |
| } else { | |
| return $dataNotificacao->format('d/m/Y \à\s H:i'); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment