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)
- by Jordan Parker
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.