SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Evangelist Kylecl's Avatar
    Join Date
    Nov 2003
    Location
    Vancouver, Canada
    Posts
    432
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Selecting where datetime field > last week date

    I'm having a problem running a query that selects all rows where their datetime value (Y-m-d H:i:s) is greater than last week's datetime value which is derived in PHP.

    How would i go about this?

    This is what i have:

    -a rows datetime value in a datetime col type
    -last weeks date(based on the time now) in the same datetime format

    Thanks!

  2. #2
    reads the ********* Crier silver trophybronze trophy longneck's Avatar
    Join Date
    Feb 2004
    Location
    Tampa, FL (US)
    Posts
    9,854
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    when you says "derived in php", i assume you are making a mysql-compatible timestamp? if so, it's as simple as:
    Code:
    select foo
      from bar
     where some_date_field > $date_calcualted_in_php

  3. #3
    SitePoint Evangelist Kylecl's Avatar
    Join Date
    Nov 2003
    Location
    Vancouver, Canada
    Posts
    432
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    none of this is a timestamp though, it's in this format Y-m-d H:i:s.

    If it was a simple number it would be easy, but it needs to actually be recognized as a date and have some mysql function work with that.

  4. #4
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,246
    Mentioned
    59 Post(s)
    Tagged
    3 Thread(s)
    you said it was a mysql datetime datatype

    that is recognized by mysql as a date, and comparing it to a valid date string using the greater than comparison operator is a valid operation and will work
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  5. #5
    reads the ********* Crier silver trophybronze trophy longneck's Avatar
    Join Date
    Feb 2004
    Location
    Tampa, FL (US)
    Posts
    9,854
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Kylecl
    none of this is a timestamp though, it's in this format Y-m-d
    i thikn you have datetime and timestamp confused. both columns store dates in such a way that you can do equality tests and math operations on them. timestamp has an added feature that updates the value evertime a row is added or updated.

    or, if you're simply storing the the timestamps as varchar, then you need to convert the column to datetime.


Bookmarks

Posting Permissions

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