Drew
Drew

Reputation: 6862

PHP Dealing with MySQL DATETIME

I have a table where I am displaying the latest administrator notes. I am only showing the last 3 notes. I am storing the DATETIME in the database. What I want to do is be able to say, if this datetime is less than 3 days old then display this NEW text.

Now if all 3 notes are in the same day, I want only the latest one to show the NEW text, not all 3.

Here is my PHP:

<?php
    if ($stmt = $mysqli->prepare("SELECT note, date_posted, admins.first_name, admins.last_name FROM admin_notes INNER JOIN admins ON admin_notes.admin_id = admins.admin_id ORDER BY date_posted DESC LIMIT 3")) {

        $stmt->execute();
        $stmt->bind_result($note_text, $note_date, $note_fname, $note_lname);
        $stmt->store_result();

        while ($stmt->fetch()) {
            echo "<tr><td class=\"full\">$note_text<br /><span class=\"from\">".timesince($note_date)." by <strong>$note_fname $note_lname</strong></span></td></tr>";
        }
    }
?>

Can anyone help me achieve this?

Upvotes: 0

Views: 321

Answers (1)

KingCrunch
KingCrunch

Reputation: 131811

$past = new DateTime('-3 days');

// later
$note_datetime = new DateTime($note_date);

if ($note_datetime >= $past) echo 'new';

Inspired by the (in the meantime deleted) answer of rkosegi: You can change the query to

SELECT note, date_posted, admins.first_name, admins.last_name, (DATEDIFF(NOW(),`date_posted`) < 3) AS isNew FROM [..]

Now you get a new "row" isNew, that is either 1, oder 0.

Upvotes: 3

Related Questions