Solved

How do I get the next/top 5 events based on a date range?

Posted on 2012-12-21
6
201 Views
Last Modified: 2012-12-21
I have a database of events for the next 12 months.

How do I query the top 5 events based on today's date for the next 6 months?

What is the syntax for the today's date and six months from now?

I've tried this:

$query = 'SELECT TOP 5 date
                        ,startTime
                        ,endTime
                        ,title
                        ,details
                        ,room
                  FROM
                        WWW.dbo.Events
                         WHERE date BETWEEN (today) and (six months from today)
                   ORDER BY date DESC';
0
Comment
Question by:Donnie Walker
  • 3
  • 3
6 Comments
 
LVL 4

Expert Comment

by:brendonfeeley
ID: 38713617
Current date: GETDATE()

6 months from now: DATEADD(month, 6, GETDATE())
0
 

Author Comment

by:Donnie Walker
ID: 38713699
when I do that it says:

Notice: Array to string conversion

how would I use the dates in the query above?
0
 

Author Comment

by:Donnie Walker
ID: 38713737
ok, I can get the dates I need in the proper format this way:

$today = date("Y-m-d");
echo $today;

$sixmonths = mktime(0,0,0,date("m"),date("d")+180,date("Y"));
echo "<br>Six Months is ".date("Y-m-d", $sixmonths);

but when I add them to my query they say they are not columns?
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 4

Accepted Solution

by:
brendonfeeley earned 500 total points
ID: 38713741
Should be the following:

SELECT TOP 5 date ,startTime ,endTime ,title ,details ,room
FROM WWW.dbo.Events
WHERE date BETWEEN GETDATE() and DATEADD(month, 6, GETDATE())
ORDER BY date DESC';
0
 

Author Comment

by:Donnie Walker
ID: 38713749
Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[42S22]: [Microsoft][SQL Server Native Client 11.0][SQL Server]Invalid column name '$today'.' in C:\inetpub\wwwroot\db2.php:72 Stack trace: #0 C:\inetpub\wwwroot\db2.php(72): PDO->query('SELECT TOP 5 e....') #1 {main} thrown in C:\inetpub\wwwroot\db2.php on line 72
0
 
LVL 4

Expert Comment

by:brendonfeeley
ID: 38713767
Doing it that way:

$query = "SELECT TOP 5 date, startTime, endTime, title, details, room
FROM WWW.dbo.Events
WHERE date BETWEEN '" . $today . "' and '" . $sixmonths . "'
ORDER BY date DESC;"
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses four methods for overlaying images in a container on a web page
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now