Jump to content

If posted today (should work right?)


V

Recommended Posts

I'm trying to display a message next to a category that has a new post.

 

The code I'm using seems pretty logical to me but I'm not sure why it isn't working. It tells how many posts are in a topic/category and alongside with that I want to check if the post date (that uses datetime in MYSQL) is equal to the current date. If it is, show a "new post today!" message.

 

$sql = "SELECT *, COUNT(post_title) as total_posts FROM posts WHERE topic_id = {$row["topic_id"]}";				
$posts_result = $connection->query($sql) or die(mysqli_error($connection));

while ($row = $posts_result->fetch_assoc()) {	

	$today = date("F j, Y");
	$post_date=$row['post_date'];				

		//find out how many posts are in topic
		echo "(";
		echo $row['total_posts'];
		echo ")";

			//new post alert
			if ($post_date==$today) {
				echo "new post today!<br />";
			}

}//close Posts loop

:shrug:

 

Link to comment
https://forums.phpfreaks.com/topic/205189-if-posted-today-should-work-right/
Share on other sites

I think you will need to convert your phpdate to a unix timestamp and then use the FROM_UNIXTIME() function in your quary to convert it to the DATETIME format and then use the UNIX_TIMESTAMP() function in your select query to convert it back and then work with it that way.

 

This artical may help it helped me.

 

http://www.richardlord.net/blog/dates-in-php-and-mysql

@ram4nd I echoed the two. For today's date I get

 

June 19, 2010

 

and post date

 

2010-06-19 14:37:54

 

also, it's not picking the date for the most recent post within the topic/category. I used ORDER BY the most recent post in the query like this

 

$sql = "SELECT *, COUNT(post_title) as total_posts FROM posts WHERE topic_id = {$row["topic_id"]} ORDER BY post_date desc";	

 

but when echoing the post title, it shows me an older post.. I think I need the most recent post to compare the dates

 

@ bobby317 Thanks for the link! I'll try and figure out the right settings for my situation.

 

 

ram4nd that's awesome! Thanks. I have both dates formatted the same now but it's still picking up the oldest post under each category. So instead of

 

ORDER BY post_date DESC

 

I used

 

GROUP BY post_date DESC

 

but now however it displays all the post dates,  :-\ I need just the latest. Should I use a limit for the GROUP BY code?

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.