Solved

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

Posted on 2012-12-21
6
199 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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Introduction Many web sites contain image galleries; a common design for these galleries includes a page with a collection of thumbnail images.  You can click on each of the thumbnail images to see the larger version of the image.  This is easily i…
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
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…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

707 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

17 Experts available now in Live!

Get 1:1 Help Now