EchoFool Posted July 26, 2008 Share Posted July 26, 2008 I was just wondering if there was a way to compare two time stamps (one from a variable and one which is the "server time" of now, to see if the difference between the two is 1 hour or less? I know how to compare days but not sure how to compare on hours and minutes. Quote Link to comment https://forums.phpfreaks.com/topic/116726-help-with-date-comparison/ Share on other sites More sharing options...
.josh Posted July 26, 2008 Share Posted July 26, 2008 simple math...timestamp is in seconds. 60 seconds in a minute, 60 minutes in an hour... $x = 100000; $y = 90000; $z = $x - $y; // 10000 $hours = $z / 60 / 60; Quote Link to comment https://forums.phpfreaks.com/topic/116726-help-with-date-comparison/#findComment-600189 Share on other sites More sharing options...
EchoFool Posted July 26, 2008 Author Share Posted July 26, 2008 How would it work though because time stamps have this kinda format: 2008-07-26 16:05:05 not straight forward integer format with all the - and : symbols.. Also, if you wanted to grab the minutes of it do you just do: $hours = $z / 60 / 60 / 60; ? Quote Link to comment https://forums.phpfreaks.com/topic/116726-help-with-date-comparison/#findComment-600194 Share on other sites More sharing options...
.josh Posted July 26, 2008 Share Posted July 26, 2008 no, time stamps are integers of the date in seconds since the unix epoch. That's just a format. What you want to do is use strtotime() to get the timestamp from your formatted times and then use some basic math. Subtract one from the other, and you got the difference in seconds. Divide that by 60, and you have the difference in minutes, etc... Quote Link to comment https://forums.phpfreaks.com/topic/116726-help-with-date-comparison/#findComment-600206 Share on other sites More sharing options...
EchoFool Posted July 26, 2008 Author Share Posted July 26, 2008 Didn't quite seem to work for me ! :-\ I have: <?php $Date = 2008-07-26 14:50:58; $Now = 2008-07-26 16:55:58; $Date = strtotime($Date); $Date2 = strtotime($Now); $Difference = $Date - $Date2; $Difference = $Difference / 60; Echo $Difference.' minutes'; ?> Response -338079.873333 minutes It should be in the negative but not by -338,079 minutes. Quote Link to comment https://forums.phpfreaks.com/topic/116726-help-with-date-comparison/#findComment-600232 Share on other sites More sharing options...
.josh Posted July 26, 2008 Share Posted July 26, 2008 except for the fact that you didn't wrap your date/time formats in quotes, your script gave me -125 minutes $Date = "2008-07-26 14:50:58"; $Now = "2008-07-26 16:55:58"; Quote Link to comment https://forums.phpfreaks.com/topic/116726-help-with-date-comparison/#findComment-600236 Share on other sites More sharing options...
EchoFool Posted July 26, 2008 Author Share Posted July 26, 2008 Yeah i thought it might have been that but when i did this to get the two times : <?php $Get = mysql_query("SELECT Date FROM table WHERE ID='$ID'") Or die(mysql_error()); $row = mysql_fetch_assoc($Get); $Date = $row['Date']; $Now = date("Y-m-d H:i:s",time()); ?> I still got same answer. Quote Link to comment https://forums.phpfreaks.com/topic/116726-help-with-date-comparison/#findComment-600239 Share on other sites More sharing options...
.josh Posted July 26, 2008 Share Posted July 26, 2008 hmm try putting backticks around `Date` and `table` in your query. Date and table are mysql reserved words. You can use them if you put backticks around them...though it's bad practice to use reserved words like that... Quote Link to comment https://forums.phpfreaks.com/topic/116726-help-with-date-comparison/#findComment-600243 Share on other sites More sharing options...
DarkWater Posted July 26, 2008 Share Posted July 26, 2008 Wait wait wait. How is the date stored in the MySQL database? A DATETIME I hope, right? You can then do: "SELECT UNIX_TIMESTAMP(Date) as timestamp FROM table WHERE ID=$ID" Then, just do: $date = $row['timestamp']; $now = time(); Quote Link to comment https://forums.phpfreaks.com/topic/116726-help-with-date-comparison/#findComment-600245 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.