EchoFool Posted October 17, 2008 Share Posted October 17, 2008 I need help working out how to compare the difference in seconds between two time stamps... i keeps returning 0 as a result.. this is what i have at the moment: <?php $User1HitTime = 2008-10-17 17:34:42; $Date = 2008-10-17 18:39:43; If($Att == $_SESSION['Current_User']){ $Date = $Date - $User1HitTime; Echo $Date; If($Date < 2){ $Second = 1; } } ?> The Echo of $Date comes out as 0... when it should surely not be? What did i do wrong? Link to comment https://forums.phpfreaks.com/topic/128879-time-comparison/ Share on other sites More sharing options...
discomatt Posted October 17, 2008 Share Posted October 17, 2008 Are you getting these timestamps from a database? Link to comment https://forums.phpfreaks.com/topic/128879-time-comparison/#findComment-668139 Share on other sites More sharing options...
EchoFool Posted October 17, 2008 Author Share Posted October 17, 2008 $Date = server time of "now" And $User1HitTime is from a table in Timestamp format. I just put examples in to the above so some one could see what might be going wrong. Link to comment https://forums.phpfreaks.com/topic/128879-time-comparison/#findComment-668146 Share on other sites More sharing options...
xylex Posted October 17, 2008 Share Posted October 17, 2008 That code parses for you? Try <?php $User1HitTime = "2008-10-17 17:34:42"; $Date = "2008-10-17 18:39:43"; If($Att == $_SESSION['Current_User']){ $Date = strtotime($Date) - strtotime($User1HitTime); Echo $Date; If($Date < 2){ $Second = 1; } } ?> Link to comment https://forums.phpfreaks.com/topic/128879-time-comparison/#findComment-668148 Share on other sites More sharing options...
discomatt Posted October 17, 2008 Share Posted October 17, 2008 By table... do you mean a table in a MySQL database? if so, select it like this SELECT UNIX_TIMESTAMP(`dateColumn`) as `dateColumn` FROM `table`.... Then simply use $difference = abs( time() - $User1HitTime ); Link to comment https://forums.phpfreaks.com/topic/128879-time-comparison/#findComment-668153 Share on other sites More sharing options...
EchoFool Posted October 17, 2008 Author Share Posted October 17, 2008 Quote By table... do you mean a table in a MySQL database? if so, select it like this SELECT UNIX_TIMESTAMP(`dateColumn`) as `dateColumn` FROM `table`.... Then simply use $difference = abs( time() - $User1HitTime ); shall give it a try thank you. Link to comment https://forums.phpfreaks.com/topic/128879-time-comparison/#findComment-668163 Share on other sites More sharing options...
PFMaBiSmAd Posted October 17, 2008 Share Posted October 17, 2008 What is your overall goal? There is probably a way to do it much faster directly in a query. Link to comment https://forums.phpfreaks.com/topic/128879-time-comparison/#findComment-668174 Share on other sites More sharing options...
EchoFool Posted October 17, 2008 Author Share Posted October 17, 2008 To check if 2 seconds has passed from the timestamp in the database to "now". To prevent double inputs. When two seconds has not passed $Seconds = 1 (or TRUE) which i then use further down the script to stop two inputs occuring. Link to comment https://forums.phpfreaks.com/topic/128879-time-comparison/#findComment-668178 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.