Need to compare two times

Discussion in 'PHP' started by squishi, Sep 9, 2008.

  1. #1
    I need to compare a date from a database with the current date.
    If the elapsed time is greater than 24 hours, a certain action should be initiated.

    $databasetime = time(); //created in the past
    PHP:
    if (SOMESTATEMENT) {
    //elapsed time > 24 hours
    //do stuff
    }		
    else {
    //do other stuff
    }
    
    PHP:
    Can I just substract two times? Like this:
    if ((time() - $databasetime) > 24*60*60){...
    PHP:
     
    squishi, Sep 9, 2008 IP
  2. lruneh

    lruneh Peon

    Messages:
    72
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    I think this might solve your problem:

    
    $exp_date = $databasetime+3600;
    $todays_date = date("Y-m-d");
    
    $today = strtotime($todays_date);
    $expiration_date = strtotime($exp_date);
    
    if ($expiration_date > $today) {
         echo "expirationdate is greater than Today";
    } else {
         echo "expirationdate is less than Today";
    }
    
    
    Code (markup):
     
    lruneh, Sep 9, 2008 IP
  3. zerxer

    zerxer Peon

    Messages:
    368
    Likes Received:
    18
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Yes, you can just subtract the two times and compare it to a timestamp for 24 hours just like you did. It'd be the same as doing if((time() - (24*60*60)) > $databasetime), which will also work.

    I don't see lruneh's method working at all. For one, they're using strtotime() on an already created timestamp. Also, it doesn't seem to limit itself to only checking if it's 24 hours ahead. It seems like they're adding an hour to the database time and then getting the timestamp for midnight of the current day (the past midnight, not the upcoming) and then as I mentioned before trying to turn a timestamp into a timestamp using strtotime() and then checking if the database's time (plus the hour) is greater than the past midnight which means it's only checking if the database's timestamp is from today so not really checking if it's 24 hours old or anything.
     
    zerxer, Sep 10, 2008 IP