bugzy Posted August 25, 2012 Share Posted August 25, 2012 I wonder how will I able to this in php. I was able to do this in mysql using this i.item_reg >= DATE_ADD(CURDATE(), INTERVAL -3 DAY) it will show only the items that were added 3 days ago up to now. In php, I want to echo something out if an item is added 3 days ago up to now I have a column in mysql where it is name item_reg and the format is datetime. I tried this if($item_reg >= strtotime(date("Y-m-d H:i:s"),3)) { //echo something here } but I think it's far from what I'm expecting... I tried searching it but I can't find any reference.. Anyone? Quote Link to comment https://forums.phpfreaks.com/topic/267542-time-interval-in-php/ Share on other sites More sharing options...
requinix Posted August 25, 2012 Share Posted August 25, 2012 The first argument to strtotime() is what you want to do to the time given as the second argument (which defaults to right now if you don't give one). So strtotime("-3 days") to give you three days ago. But since $item_reg is a DATETIME string you have to turn the Unix timestamp that strtotime() returned into a string. That's where date() comes in. date("Y-m-d H:i:s", strtotime("-3 days")) Quote Link to comment https://forums.phpfreaks.com/topic/267542-time-interval-in-php/#findComment-1372231 Share on other sites More sharing options...
DavidAM Posted August 25, 2012 Share Posted August 25, 2012 The second parameter to strtotime is a time() value, if it is omitted, the current time is used. // $item_reg came from the database & is a database formatted DATETIME: YYYY-MM-DD hh:mm:ss if (strtotime($item_reg) >= strtotime('-3 days')) { // Registered 3 or more days ago Note that this method also considers the TIME part of the Date-Time values. I usually just use strtotime() on datetimes from the (mySql) database. It is a well-known, well-structured value and I have never had a problem with it. Note that if the value is a DATE value from the database, it will not have a TIME component, and strtotime() will apply the time component (hh:mm:ss) from the current system time. Quote Link to comment https://forums.phpfreaks.com/topic/267542-time-interval-in-php/#findComment-1372235 Share on other sites More sharing options...
bugzy Posted August 25, 2012 Author Share Posted August 25, 2012 Thank requinix and DavidAM Working now! Quote Link to comment https://forums.phpfreaks.com/topic/267542-time-interval-in-php/#findComment-1372243 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.