0
Hello!
For a chat, I want to display the date of the message only once, this being the first message only.
Example:
ONTEM
Mensagem 1
Mensagem 2
Mensagem 3
HOJE
Mensagem 1
Mensagem 2
Mensagem 3
Today, what I have is working like this:
ONTEM
Mensagem 1
ONTEM
Mensagem 2
ONTEM
Mensagem 3
HOJE
Mensagem 1
HOJE
Mensagem 2
HOJE
Mensagem 3
Today I’m using the following code:
<?php
$dt_cadastro = strtotime($linha['dt_cadastro']);
$hoje_inicio = mktime(0, 0, 0, date('m'), date('d'), date('Y'));
$hoje_fim = mktime(23, 59, 59, date('m'), date('d'), date('Y'));
$ontem_inicio = mktime(0, 0, 0, date('m'), date('d') - 1, date('Y'));
$ontem_fim = mktime(23, 59, 59, date('m'), date('d') - 1, date('Y'));
$anteontem_inicio = mktime(0, 0, 0, date('m'), date('d') - 2, date('Y'));
$anteontem_fim = mktime(23, 59, 59, date('m'), date('d') - 2, date('Y'));
?>
<?php if (($dt_cadastro >= $hoje_inicio) AND ($dt_cadastro <= $hoje_fim)): ?>
<span class="chat-data" style="position:absolute; left:45%; padding-bottom: 0px;">
<span class=""><?= 'HOJE' ?></span>
</span>
<?php elseif (($dt_cadastro >= $ontem_inicio) AND ($dt_cadastro <= $ontem_fim)): ?>
<span class="chat-data" style="position:absolute; left:45%; padding-bottom: 0px;">
<span class=""><?= 'ONTEM' ?></span>
</span>
<?php elseif (($dt_cadastro >= $anteontem_inicio) AND ($dt_cadastro <= anteontem_fim)):
?>
<span class="chat-data" style="position:absolute; left:45%; padding-bottom: 0px;">
<span class=""><?= 'ANTEONTEM' ?></span>
</span>
<?php endif; ?>
This is inside a foreach
<?php foreach ($historico as $linha):?><br>
<?php endforeach;?>
PS: If there is another alternative to make code cleaner and less repetitive, I would like you to help me too, please.
You could not who knows create an array for each day? Then when the time fits in your condition the information is saved in the array Today, for example, and when displaying the messages you only write the word once Today and makes a loop to bring all messages within that array
– R.Santos
@R.Santos, I have no idea how it would work.
– Wagner Fillio
I don’t know much about PHP @Wagnerfilho, but how are you saving the content of these variables? What kind of variable is this?
– R.Santos
If you are referring to date variables, I am not saving, I am only using in order to compare to the date I am receiving from the database
– Wagner Fillio
Put the contents of the $historico array to your question
– Clayderson Ferreira
@Claydersonferreira, you don’t need it. It was solved in the way that Thiago Santos proposed.
– Wagner Fillio