聊天时间戳与iMessage相同

时间:2015-12-01 15:49:07

标签: javascript php mysql

如果在与前一条消息相同的5分钟内发生传入/传出消息,我将如何不显示消息时间戳。 iMessage如何做到这一点。我已将它设置为每个消息现在都有时间戳的位置,但很难确定如何限制它们。我该如何设置某种过滤器?

的MySQL

$query = "SELECT *
FROM messages A
JOIN profiles B ON
B.user = A.auth
JOIN lastmessage C ON
B.user = C.user
WHERE A.recip ='$view'
ORDER BY A.time DESC
";

PHP

    $result = queryMysql($query);
    $num    = $result->num_rows;
    $delayInSeconds = 300; // 5 minutes

    for ($j = 0 ; $j < $num ; ++$j)
    {
      if ($row['time'] >= strtotime("today") && $row['lastMessageTime'] < $row['time'] - $delayInSeconds)
    echo "<strong>Today</strong>" . date(' g:i A', $row['time']);
     $row['lastMessageTime'] = $row['time'];

  if ($row['time'] >= strtotime("yesterday") && $row['lastMessageTime'] < $row['time'] - $delayInSeconds)
    echo  "<strong>Yesterday</strong>". date(' g:i A', $row['time']);
    $row['lastMessageTime'] = $row['time'];

  if ($row['time'] >= strtotime("yesterday + 3,601") && $row['lastMessageTime'] < $row['time'] - $delayInSeconds)
    echo date('D, M d, g:i A', $row['time']);
    $row['lastMessageTime'] = $row['time'];

2 个答案:

答案 0 :(得分:1)

大概你是在循环播放这些消息吗?

只需跟踪上次打印时间戳的变量,只显示> 5分钟后的当前消息时间。

例如像(伪ish)

$lastMessageTime = 0; // epoch
$delayInSeconds = 300; // 5 minutes

foreach($messages as $message)
{
    //Print message (with whatever formatting  you want)
    echo $message->message;

    // Print time, if last time displayed was over 5 minutes ago
    if($lastMessageTime < $message->time - $delayInSeconds)
    {
        //More than 5 minutes ago, print time
        echo date('D, M d, g:i A', $message->time);
        $lastMessageTime = $messsage->time;
    }

}

答案 1 :(得分:-2)

我从未使用过iMessage,但似乎你正在寻找类似这样的东西(伪代码):

$last_timestamp = '';

foreach(...){

  if($last_timestamp !== $this_timestamp){
    echo $this_timestamp;
    $last_timestamp = $this_timestamp;
  }

}

这样,只有当时间戳与上一个时间戳不同时,您才会打印时间戳。

编辑:每5分钟回合一次,您也可以这样做:

  if($last_timestamp < $this_timestamp - 5 minutes)