Skip to content
Advertisement

Date in a loop – display the first / skip the rest [closed]

A chat has a message date from an SQL database. The timestamp is formatted with php date:

JavaScript

The date is in a loop (while):-

Today has to show today and yesterday has to show yesterday. Then only the date comes. It should look something like Whatsapp.

Example:- ( $dateabove – $row[‘message_text’] ) :

JavaScript

Should look like:

JavaScript

The problem is, how can you only display the date above the first message within the loop and not on the subsequent messages on that day.

This is how my loop looks like:

JavaScript

Advertisement

Answer

Here’s a little demonstration of one possible approach. Please read the comments for a detailed explanation of what’s going on. See this eval for a live demo. You can play around with it and you can see its output.

JavaScript
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement