Mysql query - between two dates
Hiya guys
I need to produce a mysql statement that returns all records between two dates, the first of which being the current date, the second of which being in 7 days time, now i know i can use the date_add function to get the exact date of the second value, but how would the mysql query be structured?
Thanks Dudes (:)
Re: Mysql query - between two dates
This assumes you have "EntryDate" and "ExitDate" fields in your database and $startdate and $endate hold your 2 dates. :thumb:
PHP Code:
$sql = "SELECT * FROM YourTable WHERE EntryDate > '$startdate' AND ExitDate < '$enddate'";
Re: Mysql query - between two dates
I don't think that will work unless you're using UNIX timestamps, but using UNIX timestamps is a good idea in the first place if you aren't already, especially when integrating with PHP.
So, assuming you are, and you only have one "date" field in your table, not two, and want to dynamically select between the two, you can do this:
PHP Code:
<?
//set the dates
$date['now'] = time(); //current date
$date['week'] = time() + (60 * 60 * 24 * 7); //+7 days from now
//build a query
$thequery = "SELECT * FROM tablename WHERE date >= $date[now] AND date <= $date[week]";
//grab the results and then dump them out
$query = mysql_query($thequery);
$results = mysql_fetch_array($query);
?>
<pre>
<?=print_r($results);?>
</pre>
Re: Mysql query - between two dates
Quote:
Originally Posted by kows
I don't think that will work unless you're using UNIX timestamps
Any of DATE, TIME, DATETIME, or TIMESTAMP, will work in a comparison.