如何检查 Php 中的内容是否超过 7 天或 1 个月前?

How can I check if something was more than 7 days or 1 month ago in Php?

我在检查某些内容是否超过 7 天或 1 个月前时遇到了一点问题,需要帮助。 整个事情应该是一个小聊天,从数据库中读取消息。 有一个时间存放的日期,我想检查消息是否早于 7 天或 1 个月。

不幸的是,整个事情并没有像我想象的那样运作,我想知道错误在哪里。

这是我的代码:

if(!empty($daten)){
    $reversed = array_reverse($daten);

    $isweek = false;
    $ismonth = false;
    $last_created;
    foreach ($reversed as $content) {
        $username = getUserName($conn, $content->sender_userid);
        $username = array_shift($username);
        $date = date("d M",strtotime($content->created));
        $time = date("H:i",strtotime($content->created));
        $today = date("d M");
        
        if(strtotime($today) < strtotime('-30 day')){
            if($ismonth == false){    
                echo '<p class="text-center">Last Month</p>';
                $ismonth = true;
            }
        } else if(strtotime($today) < strtotime('-7 day')){
            if($isweek == false){
                echo '<p class="text-center">Last Week</p>';
                $isweek = true;
            }
        }  else if(strtotime($today) == strtotime($date)){
            $date = "Today";
            echo '<p class="text-center">Today</p>';
        } else {
            if($last_created != $date){
                $date = date("D",strtotime($content->created));
                echo '<p class="text-center">' . $date . '</p>';
            }
        }

        echo '<div class="media col-sm-8 offset-md-4">';
        echo '<div class="media-body receive">';
        echo $content->message;
        echo '<span class="time">';
        echo '<i class="fas fa-check m-r-5"></i>' . $date . ' um ' . $time;
        echo '</span>';
        echo '</div>';
        echo '</div>';

        $last_created = date("d M",strtotime($content->created));
    }
} else {
    echo '<p class="text-center">Nothing here.</p>';
}

你的$last_created总是0

strtotime($last_created)更改为strtotime($content->created)

if(strtotime($content->created) < strtotime('-7 day')){
    if($isweek == false){
        echo '<p class="text-center">Last Week</p>';
        $isweek = true;
    }
} else if(strtotime($content->created) < strtotime('-30 day')){
    if($ismonth == false){    
        echo '<p class="text-center">Last Month</p>';
            $ismonth = true;
    }
}
....

更新:

$array = [
    [
        'message' => 'Test 7',
        'created' => '2021-02-08 16:02:22'
    ],
    [
        'message' => 'Test 6',
        'created' => '2021-02-07 10:02:48'
    ],
    [
        'message' => 'Test 5',
        'created' => '2021-02-05 19:02:23'
    ],
    [
        'message' => 'Test 4',
        'created' => '2021-02-05 15:02:23'
    ],
    [
        'message' => 'Test 3',
        'created' => '2021-02-04 15:02:16'
    ],
    [
        'message' => 'Test 2',
        'created' => '2021-01-30 00:17:12'
    ],
    [
        'message' => 'Test 1',
        'created' => '2021-01-28 00:14:09'
    ]
];

$isweek = false;
$ismonth = false;
$istoday = false;

foreach($array as $output){
    $created = $output['created'];

    if(strtotime($created) >= strtotime(date('Y-m-d 00:00:00'))){
        if($istoday == false){    
            echo '<b>Today</b><br>';
            $istoday = true;
        }
    } else if(strtotime($created) >= strtotime('-7 days') && strtotime($created) < strtotime(date('Y-m-d 00:00:00'))){
        if($isweek == false){
            echo '<b>Last Week</b><br>';
            $isweek = true;
        }
    } else if(strtotime($created) > strtotime('-30 days')){
        if($ismonth == false){    
            echo '<b>Last Month</b><br>';
            $ismonth = true;
        }
    }

    echo $output['message'];
    echo '<br>';
}