Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1018131
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T10:48:46+00:00 2026-05-16T10:48:46+00:00

I’m building a small scale forum script in PHP, and I am having difficulty

  • 0

I’m building a small scale forum script in PHP, and I am having difficulty demonstrating the read/unread status of a given thread.

So, I have a particularly gargantuan query which returns the thread information, including the date it was first posted in, the date it was last posted in (if any) as well as the date it was last looked at by the currently logged in user (if such a date exists.

The query then returns three values:

  • firstPostDate
  • lastPostDate
  • lastViewDate

What I need to check is that if firstPostDate or lastPostDate are more recent than lastViewDate then to mark that thread as being new / having unread replies.

I’m having problems executing this in PHP, due to the fact that my dates are all stored as timestamps in MySQL. At first I thought I could use UNIX_TIMESTAMP to return a Unix timestamp and then compare them, but I don’t believe that I’m getting the correct results (because the timestamps are in Y-M-D H-M-S format (or similiar, I do not remember off the top of my head).

I have other queries in the site that compare two dates, and that appears to work well. Is there a way I can compare the two or three dates in a mysql query, and return a “YES” if it’s new, or “NO” if it is not? It would make my PHP markup much simpler.

Or am I going about this the wrong way?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T10:48:47+00:00Added an answer on May 16, 2026 at 10:48 am

    You can use an expression as an additional field in your result set:

    SELECT   ...
             (firstPostDate > lastViewDate OR lastPostDate > lastViewDate) AS unread
    FROM     posts;
    

    The unread field should be 1 when the thread is new / having unread replies, and 0 otherwise.

    Test case:

    CREATE TABLE posts (
        id int, 
        firstPostDate timestamp, 
        lastPostDate timestamp, 
        lastViewDate timestamp
    );
    
    INSERT INTO posts VALUES (1, '2010-01-01 12:00', '2010-01-02 12:00', '2010-01-03 12:00');
    INSERT INTO posts VALUES (2, '2010-01-03 12:00', '2010-01-05 12:00', '2010-01-04 12:00');
    INSERT INTO posts VALUES (3, '2010-01-06 12:00', '2010-01-06 12:00', '0000-00-00 00:00');
    INSERT INTO posts VALUES (4, '2010-01-07 12:00', '2010-01-07 12:00', '2010-01-08 12:00');
    

    Result:

    SELECT   id,
             (firstPostDate > lastViewDate OR lastPostDate > lastViewDate) AS unread
    FROM     posts;
    
    +------+--------+
    | id   | unread |
    +------+--------+
    |    1 |      0 |
    |    2 |      1 |
    |    3 |      1 |
    |    4 |      0 |
    +------+--------+
    4 rows in set (0.01 sec)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.