1
I have a notification button, where it shows the last requests that will come out in the system, I wanted to add the time that he has already left. Ex.: 4 minutes ago, 1 hour ago, and when it was above 24 hours it showed 1 days ago, 2 days ago.... I tried several ways to shorten the hours, but could not. In my database I save the following data:
data_record -> the day it was registered Ex.: 2017-05-18;
horario_record- > the time that was registered Ex.: 10:19:30
function tempoCorridoDois($dataHoraString2) {
$hoje2 = strtotime(date('Y/m/d H:i:s'));
$dataHora2 = strtotime($dataHoraString2);
$diferenca2 = $hoje2 - $dataHora2;
$segundos2 = $diferenca2;
$minutos2 = round($diferenca2 / 60);
$horas2 = round($diferenca2 / 3600);
$dias2 = round($diferenca2 / 86400);
$semanas2 = round($diferenca2 / 604800);
$meses2 = round($diferenca2 / 2419200);
$anos2 = round($diferenca2 / 29030400);
if ($segundos2 <= 60) {
return "1 minuto atrás.";
}else if ($minutos2 <= 60) {
return $minutos2 . ' minutos atrás.';
}else if ($horas2 <= 24) {
return $horas2 . ' horas atrás.';
}else if ($dias2 <= 7) {
return $dias2 . ' dia(s) atrás.';
}else if ($semanas2 <= 4) {
return $semanas2 . ' semanas atrás.';
}else if ($meses2 <= 12) {
return $meses2 . ' meses atrás.';
}else{
return $anos2 . ' anos atrás.';
}
}
$tempoFinal2 = tempoCorridoDois($dataregistro2.$horarioregistro2);
And the following error appears:
Fatal error: Cannot redeclare timeCorridoDois() (Previously declared in C: xampp htdocs php component requests ff_notificacao_php.php:136) in C: xampp htdocs php component requests ff_notificacao_php.php on line 136
You are saving the date in the bank in this format?
– Lucas Thibau Paulino
Already fixed @Lucasthibaupaulino
– Gabriel Filippi
Well, you can use date_diff() (ex: https://www.w3schools.com/php/func_date_date_diff.asp) or treat the date and time as a string, separating it into blocks and doing the manual calculation. Both ways work, just very careful at the time of treating this information, any little mistake makes everything stop working.
– Lucas Thibau Paulino
The edition invalidated an answer and anyway there is already a question on the subject in https://answall.com/q/83326/3635. with two answers, no need for more. If you have any extra questions besides the specific problem of comparing two dates then I recommend creating a new question.
– Guilherme Nascimento