PHP Problem Involving Dates and Times

Discussion in 'PHP' started by xentech, Aug 27, 2008.

  1. #1
    I have a database full of information, one of the field's is a datefield. Basically what I want to do is run a query which will bring up rows where the date in that field is 30days or more older then the current date.

    I have quite a lot of things and my experience in PHP is good, I would be very grateful is somebody could shed some light on this.
     
    xentech, Aug 27, 2008 IP
  2. Sam Designs

    Sam Designs Active Member

    Messages:
    474
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    60
    #2
    If datefield is the time in milliseconds. Simply get today's date in milliseconds, then take off (1000 * 60 * 60 * 24 * [days]). Then do a SQL search for anything lower or equal to that time.
     
    Sam Designs, Aug 27, 2008 IP
  3. bartolay13

    bartolay13 Active Member

    Messages:
    735
    Likes Received:
    14
    Best Answers:
    1
    Trophy Points:
    98
    #3
    Thats easy.
    Just do a sql query that will subtract the unix date and the date under the datetime fields in the database.
    Make use of the predefined functions of the sql so no coding will undergo the server-scripts.

    Post if you get the query, or i'll post it though.
     
    bartolay13, Aug 27, 2008 IP
  4. xentech

    xentech Peon

    Messages:
    190
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #4
    SELECT * FROM links WHERE `lchecked` < DATE_ADD(CURRENT_DATE,INTERVAL 30 DAY)
    Code (markup):
    //with $today being today's date
    SELECT TO_DAYS($today) - TO_DATE(NOW()) AS interval from links
    Code (markup):
    SELECT TO_DAYS(one_date) - TO_DAYS(another_date) FROM your_table
    Code (markup):
    So far I have tried these queries and tried to play around with them but could not get any of them to work. If you know the query I should be using then please show me I would be very grateful.
     
    xentech, Aug 27, 2008 IP
  5. bartolay13

    bartolay13 Active Member

    Messages:
    735
    Likes Received:
    14
    Best Answers:
    1
    Trophy Points:
    98
    #5
    try this one,

    SELECT DATE_ADD('2009-01-01', INTERVAL 6/4 HOUR_MINUTE);
    -> '2009-01-04 12:20:00'
    SELECT DATE_SUB('2005-01-01 00:00:00', INTERVAL '1 1:1:1' DAY_SECOND);
    -> '2004-12-30 22:58:59'

    if you cant still get it, use the operator functions
    add, subtract, etc..

    im really sleepy right now.. ^^
     
    bartolay13, Aug 27, 2008 IP
  6. xentech

    xentech Peon

    Messages:
    190
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Sorry couldn't get your query to work but one of my colleagues found the answer:

    SELECT * FROM links WHERE (TO_DAYS(NOW()) - TO_DAYS(datefield))>30
    Code (markup):
    Thanks anyway
     
    xentech, Aug 27, 2008 IP