tec-4 Posted October 11, 2011 Share Posted October 11, 2011 To start, I am trying to compare two timestamps: 1) one that is in my database and formatted like 2011-10-11 09:43:17 2) another that I am trying to specify for the current date and time but subtracting 3 hours Essentially I am trying to pull items in my database that are newer than 3 hours old. I think the best way to compare them is by converting both into unix timestamps, but that is where I am having the difficulty and it does not seem to be comparing as it should. What I have now: //define current date in same format as in the DB (this step is probably not needed) $date = date('Y-m-d H:i:s'); //convert to unix timestamp and subtract 3 hours... this comes out to something like 1325288942 $ago = mktime($date) - 10800; //MySQL Query $sql = "select distinct Item from `database`.`table` WHERE UNIX_TIMESTAMP(EntryDate) >= '$ago'"; When I run this query it does not seem to pull any items, even if I subtract multiple days off of the $ago variable. However if I switch it to less than or equal to it pulls up all items...so not sure if my logic is off a bit or something. Thanks for the help! Quote Link to comment https://forums.phpfreaks.com/topic/248919-is-this-valid-query-to-pull-items-newer-than-a-unix-timestamp/ Share on other sites More sharing options...
Pikachu2000 Posted October 11, 2011 Share Posted October 11, 2011 No need to convert to unix time stamps at all. SELECT field FROM table WHERE EntryDate >= DATE_SUB( NOW(), INTERVAL 3 HOUR ) Quote Link to comment https://forums.phpfreaks.com/topic/248919-is-this-valid-query-to-pull-items-newer-than-a-unix-timestamp/#findComment-1278358 Share on other sites More sharing options...
tec-4 Posted October 11, 2011 Author Share Posted October 11, 2011 Okay, great. That is actually seeming to work really well - just have to tack on an extra 2 hrs to accommodate for timezones but that didn't take too long to figure out. Thanks for the help, Pikachu Quote Link to comment https://forums.phpfreaks.com/topic/248919-is-this-valid-query-to-pull-items-newer-than-a-unix-timestamp/#findComment-1278383 Share on other sites More sharing options...
fenway Posted October 11, 2011 Share Posted October 11, 2011 If EntryDate were a TIMESTAMP, MySQL would handle the timezones for you. Quote Link to comment https://forums.phpfreaks.com/topic/248919-is-this-valid-query-to-pull-items-newer-than-a-unix-timestamp/#findComment-1278405 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.