"echo" the date if it is different from the previous one

0

The whole problem is for a chat.

I have a echo , within a foreach , and for each message, get the complete date: Example:

Sat, 21 of July 2018

and I want to know how to put a check to see if the following message has the same publication date and if it has the same date, do not print it

I want to achieve something WhatsApp , that the date is not printed again, if the message was written on the same day.

My code:

foreach($requests as &$req) {
        $val1 = date('D\, j \of F Y', $req['upTime']); // da formato como el ejemplo
        $b = date('D', $req['upTime']); // de la misma fecha saca el día
        if ($val1 != $b){ // mira si no es la misma fecha
            echo "<div class='datee'><span class='date'>".$az."</span></div>";
        }
        $qg = date('H:i',$req['upTime']); // la fecha del mensaje en minutos y segundos
    
asked by Aspoky 22.07.2018 в 00:50
source

1 answer

0

I understand that the messages are ordered by date. If so, it would be enough to check the date of the current message and the previous message. Only if they are different would the message be printed.

Something similar to this:

$i = 0;
foreach($requests as &$req) {
    $printdate = true; //Siempre imprimimos la fecha del primer mensaje
    if ($i > 0) {
        $req_anterior = $requests[$i-1];
        $date_anterior = date('D\, j \of F Y', $req_anterior['upTime']);
        $date = date('D\, j \of F Y', $req['upTime']);
        $printdate = $date != $date_anterior;       
    }

    if ($printdate) {echo "<div class='datee'><span class='date'>".$az."</span></div>";}

    $i++;
}
    
answered by 22.07.2018 в 01:37