1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

PHP and MYSQL query to display records between 2 dates

Discussion in 'PHP' started by chuckylefrek, Jun 4, 2008.

  1. #1
    my database stores the date for a record as type date (0000-00-00) with the name 'dateOfSale'

    Now I have a form that allows a user to select a start date and and end date using a javascript calendar. The dates returned from this are in the format 03 Jun 2008

    So I have the following variables for example

    $fromDate = 03 Jun 2008
    $toDate = 05 Jun 2008

    And here is the query I am trying to get to work

    
    
    $query="select * FROM sales WHERE (dateOfSale BETWEEN STR_TO_DATE($fromDate, '%d %m %Y') AND STR_TO_DATE($toDate, '%d %m %Y'))";
    
    
    Code (markup):
    Thanks!
     
    chuckylefrek, Jun 4, 2008 IP
  2. EricBruggema

    EricBruggema Well-Known Member

    Messages:
    1,740
    Likes Received:
    28
    Best Answers:
    13
    Trophy Points:
    175
    #2
    Why using those dates, try to convert them to unix time and then query your database with something like this

    SELECT * FROM table WHERE unix_timestamp(datefield) >= yourdate AND unix_timestamp(datefield) <= yourdate
     
    EricBruggema, Jun 4, 2008 IP
  3. Freud2002

    Freud2002 Peon

    Messages:
    29
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Or you can use simple string comparison, as the dates are in YYYY-MM-DD format, so a simple query like this :

    SELECT * FROM sales WHERE dateOfSale>='2008-03-01' AND dateOfSale<='2008-03-30'

    should work, and give you all the sales between March 1st and March 30th
     
    Freud2002, Jun 4, 2008 IP
  4. chuckylefrek

    chuckylefrek Peon

    Messages:
    9
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Thanks for the replies but unfortunately still can't get it to work.

    Eric: I have tried the following with no joy.

    $query="select * FROM sales WHERE dateOfSale >= unix_timestamp($fromDate) AND dateOfSale <= unix_timestamp($toDate) ";

    Freud: Only the date in the database is stored as YYYY-MM-DD format. The dates for the range are in the format 05 Jun 2008

    So here are some example values for the 3 date variables

    dateOfSale: 2008-06-04

    fromDate: 05 Jun 2008
    toDate: 12 Jun 2008

    I could really do with some help solving this as I have tried so many variations of syntax and ddate formats
     
    chuckylefrek, Jun 4, 2008 IP
  5. Freud2002

    Freud2002 Peon

    Messages:
    29
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Well then you'll have to make a conversion of the dates in format 05 Jun 2008 (not really hard) to YYYY-MM-DD format, and then it'll work ;)
     
    Freud2002, Jun 4, 2008 IP