Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Limit Query by date

Status
Not open for further replies.

WilliamMute007

Programmer
Sep 30, 2007
116
GB
Hi Geniuses (if there such a word) :)

I am extremely clueless as to how to even start this much less get it working hence I will appreciate help please.

I have a staff rota which alternates. I want to display the next two record closest to today's date. i.e Today is sunday 11th January 2009. Display the next 2 weeks rota only, even though we have rota going all the way to december. I have the data in my DB and the dates are stored in using the DATE field on my MySQL DB.

How can I now use combination of MySQL and PHP to only display the next 2 weeks worth of rota?

Thank you a billion bunch in advance...
 
Start by selecting some rows - we'll just grab every column but you should probably just grab what you actually need.
Code:
SELECT * FROM table

Then add a order clause

Code:
ORDER BY `date` ASC

and then limit your results to just 2

Code:
LIMIT 2

Tek-Tips Forums is Member Supported. Click Here to donate

<honk>*:O)</honk>

Tyres: Mine's a pint of the black stuff.
Mike: You can't drink a pint of Bovril.
 
This comes down to how your query your DB. somethng along the lines of

SELECT *FROM mytable WHERE mydate BETWEEN '2009-01-11' and '2009-02-12';

----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Actually for my example you would also need a where clause that only selected from today forwards

Code:
SELECT * from table WHERE `date`>NOW() ORDER BY `date` ASC LIMIT 2

Tek-Tips Forums is Member Supported. Click Here to donate

<honk>*:O)</honk>

Tyres: Mine's a pint of the black stuff.
Mike: You can't drink a pint of Bovril.
 
Fantastic guys! I havent tried them yet, I will soon but the last post looks perfect! Thank you both for your valued contribution.
 
You might need to change ASC to DESC

Tek-Tips Forums is Member Supported. Click Here to donate

<honk>*:O)</honk>

Tyres: Mine's a pint of the black stuff.
Mike: You can't drink a pint of Bovril.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top