Jump to content

Compare two dates - post_date & last_visit


eevan79

Recommended Posts

How to compare two date from different tables? For example I want to show new posts (from posts table) till last login (from users table).

 

Here is my code, but it show all posts:

 

$result2 = mysql_query("SELECT posts.post_topic, posts.post_id, posts.post_content, posts.post_date, posts.post_by, users.user_id, users.user_name, users.user_last_login FROM posts, users WHERE users.user_last_login > posts.post_date ORDER BY post_date DESC");
while($row2 = mysql_fetch_assoc($result2))...

 

Here is time formats from tables:

post.post_date:        2010-06-28 02:44:25

users.user_last_login: 2010-07-05 02:16:38

Link to comment
Share on other sites

Here is how I solve it.

First I select users table

$result = mysql_query("SELECT * FROM users WHERE user_id = ".$_SESSION['user_id']."");...
$lastvisit_day = strtotime(($row['user_last_login']))

 

Than I select posts table:

$result2 = mysql_query("SELECT * FROM posts WHERE post_date >= NOW() - INTERVAL 30 DAY ORDER BY post_date DESC");

and compare dates with php

if ($lastvisit_day < strtotime(($row2['post_date'])))
...

Is there better way?

Link to comment
Share on other sites

$result2 = mysql_query("SELECT posts.post_topic, posts.post_id, posts.post_content, posts.post_date, posts.post_by, users.user_id, users.user_name, users.user_last_login FROM posts, users WHERE users.user_last_login > posts.post_date ORDER BY post_date DESC");
while($row2 = mysql_fetch_assoc($result2))...

 

You have no user_id selected in this query, so it selects for all users' login dates.

 

Link to comment
Share on other sites

SELECT posts.post_topic, posts.post_id, posts.post_content, posts.post_date, posts.post_by, users.user_id, users.user_name, users.user_last_login 

FROM posts, users 

WHERE users.user_last_login > posts.post_date 
AND users.user_id = ".$_SESSION['user_id']." ORDER BY post_date DESC

 

Search found 0 maches.

 

Script in my second post working, but filtered with php. Can I do this in mysql?

 

For example: I have 2 tables: posts and users. I want to select all posts with newer post_date than users.user_last_login date or select post_date between now() and users.user_last_login???

Cant make this to work (exept with php filtering).

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.