0

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?

1
  • Does timesince() return New or are you wanting to output this? You're exact question is unclear. Commented Jan 4, 2012 at 21:30

1 Answer 1

3
$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.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for your answer. I couldn't get the top PHP part to work, it was showing "new" for the last one which was 3 days old, not for the other two which were 1 and 2 days old.
As for the query, is there a way to only make the latest one show NEW? I don't want more than 1 NEW to show at all times even if they are all less than 3 days old.
First Comment: obviously it must be >= and not <=. My fault. Second comment: without UNION (which is slightly oversized here) I don't think.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.