PHP function to only view info added after certain date

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
leifsod
Forum Newbie
Posts: 5
Joined: Fri May 01, 2009 8:22 pm

PHP function to only view info added after certain date

Post by leifsod »

Hello all!

This is my first post here, and I'm brand new at PHP/MySQL, so please bare with me :)

I have a database with four columns: id, header, text and date, which I display with a while loop. Now, what I need to do is some kind of (additional?) loop to make only posts where the date variable is > today's date show. So, something like while ($date > $now), echo $row... how do I proceed?

Thanks in advance for any help!

// Leif
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: PHP function to only view info added after certain date

Post by requinix »

Just change the query:

Code: Select all

SELECT * FROM `table` WHERE "some date" >= `date`
That'll only return rows that are newer than (or as recent as) "some date". The string can be replaced with NOW() if you're looking at dates starting with today.
leifsod
Forum Newbie
Posts: 5
Joined: Fri May 01, 2009 8:22 pm

Re: PHP function to only view info added after certain date

Post by leifsod »

Thanks for your quick reply, tasairis!

I actually managed to fix it (before having seen your suggestion) by adding an if- loop after the while-loop telling it only to display rows if the variable "date" is > the current date :) . I will definitely use your tip somewhere else, though!

// Leif
ldougherty
Forum Contributor
Posts: 103
Joined: Sun May 03, 2009 11:39 am

Re: PHP function to only view info added after certain date

Post by ldougherty »

Just an FYI that is bad coding practice that you should avoid. If you are only going to use the information where "date" is > "current date" then you should use the method supplied by tasairis. The way you are doing so you are pulling extra information on a query that you do not need and then having nested while/if/etc etc is just extra CPU and memory your server will use.
leifsod
Forum Newbie
Posts: 5
Joined: Fri May 01, 2009 8:22 pm

Re: PHP function to only view info added after certain date

Post by leifsod »

Really? Had no idea! The only programming I've done is the MATLAB assignments at Uni, and there, noone really cared if we followed any practice or not...:P

I will definitely try the other way to make it as good as possible! Thanks a bunch!

Best Regards // Leif
Post Reply