westman Posted July 20, 2013 Share Posted July 20, 2013 hi all, I am trying to subtract one time from another. here is what i have.... In my DB i have datetime (2013-07-19 16:36:18) $time_to_subtract = '1';$time1 = date("Y-m-d",time() - (60*60*$time_to_subtract));if ($time1 > $time_in_DB){$update = 'Hello world';} here i am trying to take 1 hour away from the time in my DB.I have tried testing my script but i have to wait over 24 hours to see the outcome. any help? Link to comment Share on other sites More sharing options...
niche Posted July 20, 2013 Share Posted July 20, 2013 Here's the list if available functions and page on the time function http://php.net/manual/en/function.time.php Link to comment Share on other sites More sharing options...
westman Posted July 20, 2013 Author Share Posted July 20, 2013 thank you for the linkbut it was not that helpfulcould you give a code example of my problem? Link to comment Share on other sites More sharing options...
niche Posted July 20, 2013 Share Posted July 20, 2013 (edited) I'd start with your $time var. It doesn't look like someting you can subtract when you echo it. Edit: You need to focus on UNIX time. Reread the PHP link. You'll see it. Edited July 20, 2013 by niche Link to comment Share on other sites More sharing options...
jeffman Posted July 20, 2013 Share Posted July 20, 2013 strtotime("2013-07-19 16:36:18"); This will return a Unix timestamp. Timestamps are given in seconds. From this timestamp you can subtract 60*60 seconds and create a timestamp one hour earlier than your previous time. With that, you can use date() to convert the new date to any format you like. With new versions of PHP, you can also use the DateTime class. Link to comment Share on other sites More sharing options...
niche Posted July 21, 2013 Share Posted July 21, 2013 westman needs to know that his $time1 isn't going to produce the timestamp he might think. Link to comment Share on other sites More sharing options...
westman Posted July 21, 2013 Author Share Posted July 21, 2013 ok my new code is... $time_in_DB = strtotime($row['date']);$time_to_subtract = '1';$time1 = date("Y-m-d",time() - (60*60*$time_to_subtract));if ($time1 > $time_in_DB){$update = 'Hello world';} will this work? Link to comment Share on other sites More sharing options...
niche Posted July 21, 2013 Share Posted July 21, 2013 (edited) Does it? It's a simple little script. Please tell us. Edited July 21, 2013 by niche Link to comment Share on other sites More sharing options...
westman Posted July 21, 2013 Author Share Posted July 21, 2013 i have to wait over an hour to get the result and sometimes longer Link to comment Share on other sites More sharing options...
Ingolme Posted July 21, 2013 Share Posted July 21, 2013 One problem is that $time1, $time_to_subtract and $time_in_DB are all strings. String comparison works much different than number comparison. If you want to do a test that takes less time, just subtract minutes instead of hours. (instead of 60*60, just try 60) Since date() turns your timestamp back into a string, we don't want it. $time_in_DB = strtotime($row['date']); // Database time is now an integer timestamp$time_to_subtract = 1; // No quotation marks because this is a number, not a string$time1 = time() - (60*60*$time_to_subtract); // No need for date() because that would turn your number back into a string, which we don't want. // The rest is just fineif ($time1 > $time_in_DB){$update = 'Hello world';} Just to save some processing time, you can do the calculation yourself rather than make the computer do it: Instead of 60*60, use 3600 2 Link to comment Share on other sites More sharing options...
niche Posted July 21, 2013 Share Posted July 21, 2013 Right on point Ingolme. Link to comment Share on other sites More sharing options...
westman Posted July 21, 2013 Author Share Posted July 21, 2013 Ingolme, thank you so much for explaining each line, it's so helpful and has saved a lot of time. one last question... if $time1 = time() - (60*60*$time_to_subtract); works for 1 hour can i use $time1 = time() - (60*60*24*$time_to_subtract); to last for 1 or 2 days? Link to comment Share on other sites More sharing options...
Ingolme Posted July 21, 2013 Share Posted July 21, 2013 Yes, that would work for days and if you multiply by seven it would be weeks, but like I said, better do the calculation yourself and put the result in your code. A day is 86400 seconds. 1 Link to comment Share on other sites More sharing options...
westman Posted July 21, 2013 Author Share Posted July 21, 2013 thank you again Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now