Jump to content

Is this valid? Query to pull items newer than a unix timestamp


tec-4

Recommended Posts

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!

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.