【问题标题】:Date in a loop - display the first / skip the rest [closed]循环中的日期-显示第一个/跳过其余的[关闭]
【发布时间】:2020-11-01 11:40:37
【问题描述】:

聊天具有来自 SQL 数据库的消息日期。时间戳用php日期格式化:

 $dateabove = date("d.m.Y : H:i",$row['message_time']);

日期在循环中(while):-

今天必须显示今天,昨天必须显示昨天。然后只有日期来了。 它应该类似于 Whatsapp

示例:- ($dateabove - $row['message_text']):

me: 28.10.2020 : 13:75  - Hello

you: 28.10.2020 : 14:01  - Hello

me: 31.10.2020 : 11:01  - How are you?

you: 31.10.2020 : 12:01  - fine

me: 01.11.2020 : 10:05  -  Where you from?

应该看起来像:

*28.10.2020*

me: 28.10.2020 : 13:75  - Hello

you: 28.10.2020 : 14:01  - Hello


*Yesterday*

me: 31.10.2020 : 11:01  - How are you?

you: 31.10.2020 : 12:01  - fine


*Today*

me: 01.11.2020 : 10:05  -  Where you from?

问题是,如何只在循环中的第一条消息上方显示日期,而不在当天的后续消息中显示日期。

这就是我的循环的样子:

while ($row = $db->sql_fetchrow($result));

for ($i = 0, $size = sizeof($rowset); $i < $size; $i++)
{
echo $row['message_time']);
}

【问题讨论】:

  • 请通过向我们展示生成当前输出的代码来提供更多上下文。
  • 在迭代过程中存储日期,如果与以前相同则不显示。可能类似于if($row['date'] != $last_iteration_date) { echo '....';} 您的should be 在每个示例中都有日期,所以我认为您只是尝试为每个组添加一次Today/yesterday/etc 标题..
  • @RoAchterberg 代码太长,我试图让答案尽可能短。如何获取 last_iteration_date?
  • @Lars Vegas 所有聊天和日期都存储在数据库中??
  • @KUMAR 是的,所有聊天和日期都在数据库中,并且它们都已经在 while 循环中了。非常感谢您的帮助。我认为获得昨天和今天的东西对我来说没有问题。对我来说,仅将日期放在每天的第一个帖子上方并跳过其余部分非常重要。

标签: php for-loop while-loop iteration


【解决方案1】:

这里有一个可能的方法的小演示。请阅读 cmets 以获取有关正在发生的事情的详细说明。有关现场演示,请参阅 this eval。你可以玩弄它,你可以看到它的输出。

<?php

// Current time as UNIX timestamp.
$now = time();
// Find yesterday's date by subtracting one day ('P1D') from today (new DateTime).
$yesterday = (new DateTime)->sub(new DateInterval('P1D'));

// Mock a bunch of messages.
$messages =
[
    // Yesterday's messages
    [
        'user_id' => 1,
        'user_natural_name' => 'me',
        // format('U') will give us UNIX timestamp.
        'timestamp' => $yesterday->setTime(19, 0)->format('U'),
        'text' => 'Hiya'
    ],
    [
        'user_id' => 2,
        'user_natural_name' => 'you',
        'timestamp' => $yesterday->setTime(19, 5)->format('U'),
        'text' => 'Hey, how are you?'
    ],
    
    // Today's messages
    [
        'user_id' => 1,
        'user_natural_name' => 'me',
        'timestamp' => $now,
        'text' => 'Hello'
    ],
    [
        'user_id' => 2,
        'user_natural_name' => 'you',
        // Three seconds later.
        'timestamp' => $now + 3,
        'text' => 'Hi'
    ],
    [
        'user_id' => 1,
        'user_natural_name' => 'me',
        'timestamp' => $now + 6,
        'text' => 'What\'s up'
    ],
    [
        'user_id' => 2,
        'user_natural_name' => 'you',
        'timestamp' => $now + 9,
        'text' => 'Not much'
    ],
    [
        'user_id' => 1,
        'user_natural_name' => 'me',
        'timestamp' => $now + 12,
        'text' => 'Awesome'
    ]
];

// If $lastWindowDate differs from the current message in the queue,
// then display it as a separator between day-to-day conversations.
$lastWindowDate = NULL;

// $index will tell us whether we already had output from earlier days so we can
// show a separating newline.
foreach ($messages as $index => $message)
{
    // Window as a perspective on the message flow.
    $windowDate = date('Ymd', $message['timestamp']);
    // If current message's date is not last message's date, then output it to the user.
    if ($windowDate !== $lastWindowDate)
    {
        // Translate today's YMD and yesterday's YMD to something more naturally interpretable for a better UX.

        // Compare against current message's datestamp.
        switch (date('Ymd', $message['timestamp']))
        {
            // date('Ymd') without a second argument defaults to 'today'.
            case date('Ymd'):
                $dateLabel = 'today';
            break;
            // Format $yesterday DateTime as Ymd.
            case $yesterday->format('Ymd'):
                $dateLabel = 'yesterday';
            break;
            // Neither? Pass through as numeric date.
            default:
                $dateLabel = $windowDate;
            break;
        }
        
        // Already had previous output? Show a newline now as visual separator.
        if ($index)
            echo "\n";

        // Output date label at the top of message list for that day.
        echo "$dateLabel\n";
        
        // Reset the last date seen for the conditional to work.
        $lastWindowDate = $windowDate;
    }
 
    // Show message.   
    echo "{$message['user_natural_name']}: " . date('d.m.Y : H:i', $message['timestamp']) . " - {$message['text']}\n";
}

【讨论】:

  • 太好了,让我知道结果如何。
  • 嗯,你的演示效果很好,这正是我想要的,但在我的情况下,它不会跳过日期。我不想浪费你的时间,因为你已经让我成为了一个很好的例子。也许我可以给你看我的漏洞脚本?
  • @LarsVegas,如果您的意思是我在消息输出中省略了日期,那么您是对的。我更新了答案以包含日期,以便更好地模仿您的示例输出。如果您觉得我的回答有帮助,请考虑将其标记为已接受 :)。
  • 非常感谢您的快速回复。不,我的意思是我不能让你的例子适应我的代码。无论如何,当您的答案与我的问题不符时,我会接受您的回答,因为这是我的错。但也许你可以看看我的代码?我将它粘贴到这里,请查看循环中的 $row['message_time']。 pastebin.com/xwzgQ1jx
  • 我现在已经接受了你的回答,但就像我说的那样,如果你能看看我的代码并为我提供解决方案,那就太好了?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-06-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-01-05
  • 1970-01-01
相关资源
最近更新 更多