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!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.