Selecting Current Date Data

Discussion in 'Databases' started by neilfurry, Jul 7, 2016.

  1. #1
    hi,
    im not sure if this is a simple question but it makes me wonder.

    i have a field on my database containing date that a service is created.
    example:

    date_created
    2016-07-07 09:52:15
    2016-07-07 08:42:25
    2016-07-07 07:22:35
    2016-07-06 09:12:15
    2016-07-06 07:22:25

    On my form i have two datepickers 1. startdate 2. enddate
    supposed i selected 2016-07-07 from datepicker 1 and 2
    it should return data that was created 2016-07-07

    here is my query:

    SELECT * FROM mytable WHERE date_created<=2016-07-07 AND date_created<=2016-07-07

    this query does not return any data.

    can you check what is wrong?
     
    neilfurry, Jul 7, 2016 IP
  2. sarahk

    sarahk iTamer Staff

    Messages:
    28,807
    Likes Received:
    4,534
    Best Answers:
    123
    Trophy Points:
    665
    #2
    Read through your query, you are always looking for the date created earlier than the selected date.

    It should be

    SELECT * FROM mytable WHERE date_created <= 2016-07-07 AND date_created >= 2016-07-07

    but it's probably easier to visualise if you put in the variable names from your datepickers

    $sql = "SELECT * FROM `mytable` WHERE `date_created` <= '{$enddate}' AND `date_created` >= '{$startdate}'";
     
    sarahk, Jul 7, 2016 IP