PHP MySQL循环遍历行并在日期更改时打印日期

Posted

技术标签:

【中文标题】PHP MySQL循环遍历行并在日期更改时打印日期【英文标题】:PHP MySQL loop through the rows and print date when the date changes 【发布时间】:2021-09-18 06:50:04 【问题描述】:

我的messages 表是这样的:

id : (auto increment)
incoming_msg_id : (receiver's user id, BIGINT)
outgoing_msg_id : (sender's user id, BIGINT)
msg : (message content, LONGTEXT)
created_at : (timestamp)

这是我用于获取消息的查询:

$stmt = $pdo->prepare("SELECT * FROM messages
                       LEFT JOIN users ON users.id = messages.outgoing_msg_id
                       WHERE (outgoing_msg_id = :omsg AND incoming_msg_id = :imsg) OR (outgoing_msg_id = :imsg AND incoming_msg_id = :omsg)
                       ORDER BY messages.id ASC");
$stmt-> bindValue(':imsg', $imsg);
$stmt-> bindValue(':omsg', sessionUser());
$stmt-> execute();

我想使用created_at 时间戳字段对消息进行分组,以便在消息事务发生时显示date

例如:

5th July, 2021

Message 1
Message 2

6th July, 2021

Message 3
Message 4

因此,我想在每次日期更改时打印一个包含日期的新标题。我当前的 php 循环是这样的:

while($f = $stmt->fetch())

  // HERE, I WANT TO CHECK IF DATE CHANGES AND PRINT A NEW HEADER, FOR EXAMPLE
  // if(dateChanges) echo date('jS M, Y', strtotime($f['created_at']));  // UNSURE, OF LOGIC INSIDE IF STATEMENT

  if($f['outgoing_msg_id'] == sessionUser())
    $html .= "<div class='chat outgoing'>
                <div class='details d-flex'>
                  <div class='textmsgbox'>
                    <div class='thechatmsg'>".decrypt($f['msg'], ENCRYPTION_KEY)."</div>
                    <div class='tmbcontent d-flex'>
                      <small>".date('h:i a', strtotime($f['created_at']))."</small>
                      <small class='seen'>$seen</small>
                    </div>
                  </div>
                </div>
              </div>";
  else
    $html .= "<div class='chat incoming'>
                <div class='details d-flex'>
                  <div class='textmsgbox'>
                    <div class='thechatmsg'>".decrypt($f['msg'], ENCRYPTION_KEY)."</div>
                    <div class='tmbcontent'>
                      <small>".date('h:i a', strtotime($f['created_at']))."</small>
                    </div>
                  </div>
                </div>
              </div>";
  

【问题讨论】:

您需要将当前行的日期存储在一个变量中。然后,下次循环时,您可以将变量与新的当前日期进行比较,看看它是否已更改 @ADyson 我认为 Shreyansh Kashyap 的回答完全符合您的建议。谢谢:) 【参考方案1】:

非常简单。首先,您需要在循环外声明一个null 变量,然后对照它检查时间戳的日期并打印日期。然后最后您可以使用时间戳中的日期更新先前定义的null 变量,然后您就完成了。看看下面的代码:

$lastDate = null; // DECLARE A NULL VARABLE FOR THE LAST DATE
while($f = $stmt->fetch())
  $chatDate = null; // DECLARE A NULL VARIABLE HERE TO AVOID MULTIPLE OUTPUTS
  
  // CHECK AGAINST THE LAST DATE IF IT EQUALS TO THE TIMESTAMP'S DATE, IF NOT, PRINT THE DATE
  if($lastDate !== date('Y-m-d', strtotime($f['created_at']))) 

    // I ADDED THIS ADDITIONAL CODE SO THAT YOU CAN PRINT THE DATE AS "TODAY" & "YESTERDAY" 
    // You may remove this if you don't want

    if(date('Y-m-d', strtotime($f['created_at'])) == date('Y-m-d'))
      $cd = "TODAY";
    else if(date('Y-m-d', strtotime($f['created_at'])) == date('Y-m-d', strtotime('-1 day')))
      $cd = "YESTERDAY";
    else
      $cd = date('jS F, Y', strtotime($f['created_at']));
    

    $chatDate = "<div class='chat-date'>".$cd."</div>";
  

  $html .= $chatDate;

  // YOUR IF....ELSE CONDITION GOES HERE
  if()
    // Your code as in question
  else
    // Your code as in question
  

  // UPDATE THE NULL VARIABLE WITH CURRENT DATE
  $lastDate = date('Y-m-d', strtotime($f['created_at'])); 

完成!就如此容易。享受:)

【讨论】:

像魅力一样工作...另外,TODAYYESTERDAY 支票加 1 :) 谢谢!

以上是关于PHP MySQL循环遍历行并在日期更改时打印日期的主要内容,如果未能解决你的问题,请参考以下文章

使用 django 查询返回活动时区中的日期时间

Windows 批处理文件 - 循环遍历日期范围数组,然后拆分每个项目

SQLAlchemy:循环遍历模型对象以复制它们时的KeyError

如何在 MySQL 数据库中显示日期和价格,并在 PHP 中以各种格式显示

PHP脚本循环遍历目录中的所有文件?

更改html表中的日期类型 - php mysql [重复]