How can the issue of incorrect message ordering in a chat application be resolved using PHP?

Issue: Incorrect message ordering in a chat application can be resolved by adding a timestamp to each message when it is sent, and then sorting the messages based on this timestamp before displaying them to the user.

// Assuming $messages is an array of messages with 'timestamp' field
usort($messages, function($a, $b) {
    return $a['timestamp'] - $b['timestamp'];
});

foreach ($messages as $message) {
    echo $message['content'] . "<br>";
}