0

I am trying to get the difference in seconds between the date/time in the column 'last_visited' in my table 'users' and the current date/time for a particular user. I think the following query should do it:

$query ="SELECT unix_timestamp(NOW()) - unix_timestamp(last_visit) from users WHERE username='$user'";
$result=mysql_query($query) or die(mysql_error());

However, I am not sure how to get the result from this as a variable so that I can pass it to a PHP function. Could someone help with this?

Nick
  • 4,304
  • 15
  • 69
  • 108
  • have you looked at the `$result`variable using `var_dump` ? Also, please don't use the `mysql_*` methods, there deprecated. – Florian Aug 12 '12 at 21:32

2 Answers2

1
$query ="SELECT unix_timestamp(NOW()) - unix_timestamp(last_visit) AS time_difference from users WHERE username='$user'";
                                                                   ^

I guess it will be easier to use this variable if it has a proper name.

Tim
  • 782
  • 1
  • 5
  • 11
  • Thanks. How do I then pass 'time_difference' to my 'time_since' function as in `$visited = time_since (*SECONDS NEED TO GO HERE*);` – Nick Aug 12 '12 at 21:37
  • **c 2**'s comment above tells how to read data from the query result. – Tim Aug 12 '12 at 21:42
  • @Tim: Always link. What's above or below varies, because this site sometimes prefers your answer first, than the other. And welcome to SO! – hakre Aug 12 '12 at 21:52
1

Return the result with a name:

$query ="SELECT unix_timestamp(NOW()) - unix_timestamp(last_visit) AS time_diff from users WHERE username='$user'";
$result=mysql_query($query) or die(mysql_error());

while($row=mysql_fetch_assoc($result)) 
{ 
 $diff= $row['time_diff']
}
c 2
  • 1,127
  • 3
  • 13
  • 21