简体   繁体   中英

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

I have a little problem with checking if something is longer than 7 days or 1month ago and need help. The whole thing should be a small chat, where the messages are read from the database. There is a date with time deposited where I like to check whether the message is older than 7 days or 1 month.

Unfortunately, the whole thing doesn't work as I imagined it would, and I wonder where the error is.

Here is my Code:

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>';
}

Your $last_created always is 0 !

Change strtotime($last_created) to 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;
    }
}
....

Update:

$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>';
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM