Dates in MySQL queries

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Mark Stevens

    #1

    Dates in MySQL queries

    I am trying to do select all of the records in a table where they data
    has not expired.

    The table contains two fields Subject (varchar(100)) and ExpiryDate
    (Date).

    I am getting all of the records returned when I execute:

    $sql = "select * from tbl where ExpiryDate > " . date("Y-m-d");
    mysql_query($sq l);

    The expiry date in the record I am looking at is 2004-03-25 and the
    query that is being executed is:

    select * from tbl where ExpiryDate > 2004-03-28

    What am I missing? Can anyone recommend a good site to look at that
    discusses dates in MySQL queries?

    Thanks in advance,
    Mark
    --
    |\ _,,,---,,_ A picture used to be worth a
    ZZZzzz /,`.-'`' -. ;-;;, thousand words - then along
    |,4- ) )-,_. ,\ ( `'-' came television!
    '---''(_/--' `-'\_)

    Mark Stevens (mark at thepcsite fullstop co fullstop uk)
  • Andy Hassall

    #2
    Re: Dates in MySQL queries

    On Sun, 28 Mar 2004 16:54:52 +0100, Mark Stevens <read@my.sig> wrote:
    [color=blue]
    >I am trying to do select all of the records in a table where they data
    >has not expired.
    >
    >The table contains two fields Subject (varchar(100)) and ExpiryDate
    >(Date).
    >
    >I am getting all of the records returned when I execute:
    >
    > $sql = "select * from tbl where ExpiryDate > " . date("Y-m-d");
    > mysql_query($sq l);
    >
    >The expiry date in the record I am looking at is 2004-03-25 and the
    >query that is being executed is:
    >
    >select * from tbl where ExpiryDate > 2004-03-28
    >
    >What am I missing?[/color]

    Quotes.

    2004-03-28 is an arithmetic expression which equals 1978. This isn't a valid
    date representation, so it either gets silently turned into the zero-date
    0000-00-00, which is less than all the dates in your table, or they're both
    turned into strings and compared, which still gives the same result. (I don't
    know off the top of my head which approach MySQL takes in this case).

    Whereas '2004-03-28' is a string representation of a date, which MySQL can use
    to compare with a date field.
    [color=blue]
    >Can anyone recommend a good site to look at that
    >discusses dates in MySQL queries?[/color]

    http://www.mysql.com/documentation/m...e_calculations
    http://www.mysql.com/documentation/m...and_time_types
    http://www.mysql.com/documentation/m...time_functions
    http://www.mysql.com/documentation/m...tml#Using_DATE

    --
    Andy Hassall <andy@andyh.co. uk> / Space: disk usage analysis tool
    http://www.andyh.co.uk / http://www.andyhsoftware.co.uk/space

    Comment

    • Andy Hassall

      #3
      Re: Dates in MySQL queries

      On Sun, 28 Mar 2004 17:05:51 +0100, Andy Hassall <andy@andyh.co. uk> wrote:
      [color=blue]
      >2004-03-28 is an arithmetic expression which equals 1978.[/color]

      1973, even. Bleh :-p

      --
      Andy Hassall <andy@andyh.co. uk> / Space: disk usage analysis tool
      http://www.andyh.co.uk / http://www.andyhsoftware.co.uk/space

      Comment

      • Mark Stevens

        #4
        Re: Dates in MySQL queries

        On Sun, 28 Mar 2004 17:05:51 +0100, Andy Hassall <andy@andyh.co. uk>
        wrote:
        [color=blue]
        > Quotes.
        >
        > 2004-03-28 is an arithmetic expression which equals 1978. This isn't a valid[/color]

        Thanks, works fine now.

        Regards,
        Mark
        --
        |\ _,,,---,,_ A picture used to be worth a
        ZZZzzz /,`.-'`' -. ;-;;, thousand words - then along
        |,4- ) )-,_. ,\ ( `'-' came television!
        '---''(_/--' `-'\_)

        Mark Stevens (mark at thepcsite fullstop co fullstop uk)

        Comment

        Working...