Results 1 to 4 of 4

Thread: Mysql query - between two dates

  1. #1

    Thread Starter
    Frenzied Member I_Love_My_Vans's Avatar
    Join Date
    Jan 2005
    Location
    In the PHP compiler
    Posts
    1,275

    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 (

  2. #2
    PowerPoster lintz's Avatar
    Join Date
    Mar 2003
    Location
    The 19th Hole
    Posts
    2,697

    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.
    PHP Code:
    $sql "SELECT * FROM YourTable WHERE EntryDate > '$startdate' AND ExitDate < '$enddate'"

  3. #3
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    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>
    Like Archer? Check out some Sterling Archer quotes.

  4. #4
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width