HokieTracks Posted June 14, 2009 Share Posted June 14, 2009 I am trying to find the difference between the current datetime and the datetime the user was last active. But for whatever reason the code isn't outputting anything but it doesn't have any errors either. $lastactive = $row_userdata['lastactive']; $now = date("Y-m-d H:i:s"); $activequery = "SELECT TIMEDIFF('$lastactive','$now') AS '$difference'"; mysql_query($activequery) or die(mysql_error()); echo $difference; Quote Link to comment https://forums.phpfreaks.com/topic/162169-trying-to-work-with-timediff/ Share on other sites More sharing options...
Ken2k7 Posted June 14, 2009 Share Posted June 14, 2009 $activequery = "SELECT TIMEDIFF('$lastactive','$now') AS '$difference'"; You're not making any sense there. Try removing the single quotes around $difference and remove the $ in $difference. :-\ Quote Link to comment https://forums.phpfreaks.com/topic/162169-trying-to-work-with-timediff/#findComment-855813 Share on other sites More sharing options...
HokieTracks Posted June 14, 2009 Author Share Posted June 14, 2009 Ok, but then how do I display "difference" in php. Quote Link to comment https://forums.phpfreaks.com/topic/162169-trying-to-work-with-timediff/#findComment-855817 Share on other sites More sharing options...
Ken2k7 Posted June 14, 2009 Share Posted June 14, 2009 Have you ever displayed a SQL result before? Same way you did with $lastactive. Quote Link to comment https://forums.phpfreaks.com/topic/162169-trying-to-work-with-timediff/#findComment-855818 Share on other sites More sharing options...
HokieTracks Posted June 14, 2009 Author Share Posted June 14, 2009 Ohhh, I see now. AS is putting the result into the table. I thought it was defining the result as a variable. Quote Link to comment https://forums.phpfreaks.com/topic/162169-trying-to-work-with-timediff/#findComment-855824 Share on other sites More sharing options...
Ken2k7 Posted June 14, 2009 Share Posted June 14, 2009 Not exactly. It just gives the column an alias name for easier reference. Quote Link to comment https://forums.phpfreaks.com/topic/162169-trying-to-work-with-timediff/#findComment-855841 Share on other sites More sharing options...
PFMaBiSmAd Posted June 15, 2009 Share Posted June 15, 2009 Executing a second query just to calculate a difference using data from a database kind of defeats the point of using the mysql functions in a query. You should be using the TIMEDIFF() logic in your first SELECT query that is getting $row_userdata['lastactive']. Also the mysql NOW() function does the same thing as your $now php code. Quote Link to comment https://forums.phpfreaks.com/topic/162169-trying-to-work-with-timediff/#findComment-855947 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.