WAMFT1 Posted January 20, 2016 Share Posted January 20, 2016 Hi all I am trying to show date different from last login to today's date. Can someone please help with where I am going wrong? <table width="730" border="0" align="center" cellpadding="4" cellspacing="0"> <tr class="beacon_heading_filled_left"> <td width="180">User</td> <td width="50">State</td> <td width="50">Adviser</td> <td width="50">STAFF</td> <td width="50">INF</td> <td width="50">FORUM</td> <td width="100">Last Login</td> <td width="50">#Days</td> <td width="50">Type</td> </tr> <?php include("../edb.php"); $result=mysql_query("SELECT * FROM `users` WHERE active='1' and UserType<>'TMP' order by LastName ASC"); while($test = mysql_fetch_array($result)) { $id = $test['id']; $timestamp = $test['Online']+36000; $current = new DateTime('now'); $diff = date_diff($current, $timestamp); echo"<tr class='beacon_standard_left'>"; echo"<td>[".$test['dlr_group']."] ".$test['LastName'].", " .$test['FirstName']."</td>"; echo"<td>".$test['State']."</td>"; echo"<td>".$test['MENUAdviser']."</td>"; echo"<td>".$test['MENUStaff']."</td>"; echo"<td>".$test['MENUINF']."</td>"; echo"<td>".$test['MENUPlatForum']."</td>"; echo"<td>" .gmdate("d M Y", $timestamp)."</td>"; echo"<td>".$diff."</td>"; echo"<td>".$test['UserType']."</td>"; echo "</tr>"; } mysql_close($conn); ?> </table> Quote Link to comment Share on other sites More sharing options...
Barand Posted January 20, 2016 Share Posted January 20, 2016 date_diff() takes two DateTime objects as parameters and returns a DateInterval object http://uk1.php.net/manual/en/datetime.diff.php Quote Link to comment Share on other sites More sharing options...
Barand Posted January 20, 2016 Share Posted January 20, 2016 You can easily get the time diff while you query the data. It looks, from your code, like you store the time as an integer unix timestamp. It is much better to us DATETIME type columns (yyyy-mm-dd hh:ii:ss) so you don't have to convert them every time you use them, plus you can read them. Here's an example of getting the time diff in hours from a unix timestamp SELECT thetime as unixtime , FROM_UNIXTIME(thetime) as realtime , NOW() as timenow , timestampdiff(HOUR, FROM_UNIXTIME(thetime)+INTERVAL 10 HOUR, NOW()) as diff FROM test_time; +------------+---------------------+---------------------+------+ | unixtime | realtime | timenow | diff | +------------+---------------------+---------------------+------+ | 1453276800 | 2016-01-20 08:00:00 | 2016-01-20 09:48:25 | -8 | | 1453284000 | 2016-01-20 10:00:00 | 2016-01-20 09:48:25 | -10 | +------------+---------------------+---------------------+------+ 2 rows in set (0.00 sec) 2 Quote Link to comment 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.