N-Bomb(Nerd) Posted May 13, 2009 Share Posted May 13, 2009 Hello, I'm creating a last seen script for my website and ran into a bit of trouble. I'm able to store when the user was last seen, and even pull that information from the database, however I have no idea how to use the DATEDIFF function with mySQL. Here's my test code so far.. Also let me know if you see a way I could improve this. function test() { if (mySQLConnect('host', 'db', 'username', 'password') == true) { $Query = mysql_fetch_array(mysql_query("SELECT * FROM TimeTable WHERE username = 'workgoddamnit'")); echo $Query['username'] . ' was last seen ' . ???? } } I'm able to format the date myself, heh just I don't know how to use DATEDIFF properly.. any help is much appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/157951-last-seen/ Share on other sites More sharing options...
N-Bomb(Nerd) Posted May 13, 2009 Author Share Posted May 13, 2009 Whoops, forgot to mention that my database consists of two fields.. "username" and "last". "last" being the last seen time. Quote Link to comment https://forums.phpfreaks.com/topic/157951-last-seen/#findComment-833142 Share on other sites More sharing options...
Ken2k7 Posted May 13, 2009 Share Posted May 13, 2009 $Query['last']? Quote Link to comment https://forums.phpfreaks.com/topic/157951-last-seen/#findComment-833215 Share on other sites More sharing options...
N-Bomb(Nerd) Posted May 13, 2009 Author Share Posted May 13, 2009 $Query['last']? Yes, that contains the time the user was last seen, however I'm trying to get the difference between the time he was last seen and the current time. Quote Link to comment https://forums.phpfreaks.com/topic/157951-last-seen/#findComment-833278 Share on other sites More sharing options...
jackpf Posted May 13, 2009 Share Posted May 13, 2009 $d = time() - $last; echo date('H:i', $d); Somethign along those lines. Quote Link to comment https://forums.phpfreaks.com/topic/157951-last-seen/#findComment-833279 Share on other sites More sharing options...
fenway Posted May 13, 2009 Share Posted May 13, 2009 DATEDIFF( yourField, NOW() ) Quote Link to comment https://forums.phpfreaks.com/topic/157951-last-seen/#findComment-833293 Share on other sites More sharing options...
N-Bomb(Nerd) Posted May 13, 2009 Author Share Posted May 13, 2009 DATEDIFF( yourField, NOW() ) I'm sorry, I've tried to mess with that for a bit now and I can't quite figure out how to get that to work in my query. Could you perhaps give me an example using that? Quote Link to comment https://forums.phpfreaks.com/topic/157951-last-seen/#findComment-833300 Share on other sites More sharing options...
kickstart Posted May 13, 2009 Share Posted May 13, 2009 Hi Put it in something like this:- function test() { if (mySQLConnect('host', 'db', 'username', 'password') == true) { $Query = mysql_fetch_array(mysql_query("SELECT username, last, DATEDIFF(last,NOW()) TimeSinceLastTouchedInDays FROM TimeTable WHERE username = 'workgoddamnit'")); echo $Query['username'] . ' was last seen ' . $Query['TimeSinceLastTouchedInDays ']; } } This will return the number of days since the were last seen as a field called TimeSinceLastTouchedInDays . All the best Keith Quote Link to comment https://forums.phpfreaks.com/topic/157951-last-seen/#findComment-833306 Share on other sites More sharing options...
N-Bomb(Nerd) Posted May 14, 2009 Author Share Posted May 14, 2009 Keith, I've tried the code that you've posted, and I can't seem to get it to work. Usually if I mess around enough with the code I can get it to work, this doesn't seem to be the case this time. Any suggestions on what I should do? I know this isn't a good reply because I didn't reply any output simply for the fact that there was none. Quote Link to comment https://forums.phpfreaks.com/topic/157951-last-seen/#findComment-833746 Share on other sites More sharing options...
kickstart Posted May 14, 2009 Share Posted May 14, 2009 Hi Do you have something like phpmyadmin to just execute the SQL in directly? If so give that a try. All the best Keith Quote Link to comment https://forums.phpfreaks.com/topic/157951-last-seen/#findComment-833917 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.