Jordan Parker
Jordan Parker

Reputation: 75

Grabbing rows from MySql where current date is in between start date and end date (Check if current date lies between start date and end date)

I'm trying to select from the database to get the "campaigns" that dates fall into the month. So far i've been successful in grabbing rows that starts or ends inside the current month. What I need to do now is select rows that start in one month and ends a few months down the line ( EG: It's the 3rd month in the year, and there's a "campaign" that runs from the 1st month until the 5th. other example There is a "campaign" that runs from 2012 until 2013 )

I'm hoping there is some way to select via MySql all rows in which a capaign may run. If not should I grab all data in the database and only show the ones that run via the current month.

I have already made a function that displays all the days inbetween each date inside an array, which is called "dateRange". I've also created another which shows how many days the campaign runs for called "runTime".

Select all (Obviously)

$result = mysql_query("SELECT * FROM campaign");

Select Starting This Month

$result = mysql_query("SELECT * FROM campaign WHERE YEAR( START ) = YEAR( CURDATE( ) ) AND MONTH( START ) = MONTH( CURDATE( ) )");

Select Ending This Month

$result = mysql_query("SELECT * FROM campaign WHERE YEAR( END ) = YEAR( CURDATE( ) ) AND MONTH( END ) = MONTH( CURDATE( ) ) LIMIT 0 , 30");

Code sample

while($row = mysql_fetch_array($result))
{
$dateArray = dateRange($row['start'], $row['end']);

echo "<h3>" . $row['campname'] . "</h3> Start " . $row['start'] . "<br /> End " . $row['end'];
echo runTime($row['start'], $row['end']);
print_r($dateArray);
}

In regards to the dates, MySql database only holds start date and end date of the campaign.

Upvotes: 3

Views: 3162

Answers (1)

Jonathan Kuhn
Jonathan Kuhn

Reputation: 15301

to detect overlap of date/times, you can do something like this:

//range of dates to get overlap. means if any row in the database has
//a day in its start/end range that falls between these days
$startDate = '2012-06-01';
$endDate = '2012-06-30';

$sql = "SELECT *
    FROM campaign
    WHERE
        `start` < '$endDate'
        AND `end` > '$startDate'";

that will find any row from campaign that has any day that falls between the $startDate and $endDate. This also works for all occurrences of campaign...:

  1. starts before $startDate and ends before $endDate (end date between date range)
  2. starts after $startDate and ends before $endDate (both start and end between date range)
  3. starts before $startDate and ends after $endDate (start and end encompass date range)
  4. starts after $startDate and ends after $endDate (start date between date range)

Also, this query as it stands is has $startDate and $endDate as exclusive. Meaning that if something starts on 31-May (based on the example dates above) and ends on 1-Jun, it would not be included. If you want the dates to be inclusive, you can change the > and < to be >= and <= to include the start and end date.

Upvotes: 4

Related Questions