Skip to content
PHP Logo

How can news from the last 3 days be displayed in PHP without using time()-259200?

To display news from the last 3 days in PHP without using time()-259200, you can calculate the timestamp for 3 days ago using the mktime() function. This function allows you to specify the hour, minute, second, month, day, and year to create a timestamp. By subtracting the timestamp for 3 days ago from the current timestamp, you can filter out news articles that are older than 3 days.

php
// Calculate the timestamp for 3 days ago
$threeDaysAgo = mktime(0, 0, 0, date("m"), date("d") - 3, date("Y"));

// Loop through news articles and display only those from the last 3 days
foreach($newsArticles as $article) {
    if(strtotime($article['date']) >= $threeDaysAgo) {
        echo "<h3>{$article['title']}</h3>";
        echo "<p>{$article['content']}</p>";
    }
}