Date: 07/21/05 (PHP Community) Keywords: mysql, sql I want to report back to the user how long it's been since their last login. I'm having a problem processing the time, though.
$query = "SELECT DATE_FORMAT(date_last,'%c-%d-%Y %k:%i:%S') dl,
DATE_FORMAT(NOW(),'%c-%d-%Y %k:%i:%S') dn FROM people WHERE
(user_id=$id AND password=PASSWORD('$pass') )";
$result = mysql_query($query);
if (mysql_num_rows($result)==1) {
while ($row=mysql_fetch_array($result)) {
$lasttime = $row['dl'];
$nowtime = $row['dn'];
$timediff = round((strtotime($nowtime) - strtotime($lasttime)) / (60*60));
echo "You last logged in $timediff hours ago.";
}
}
Here is what I get: // $nowtime == string(18) "7-21-2005 10:51:40" // strtotime($nowtime) == int(1393429900) // $lasttime == string(18) "7-20-2005 21:21:59" // strtotime($lasttime) == int(1390789319) You last logged in 733 hours ago I had understood that strtotime() will parse GNU standard date formats into seconds, so I had assumed multiplying the difference in dates by 1200 would get the difference in hours. I don't understand why I'm getting 733 hours offset instead of 13. Can anybody tell me what I've got wrong here? Edit: A saner MySQL query does the trick. Thanks to those who helped. Source: http://www.livejournal.com/community/php/323703.html
|