Jump to content

DateDiff issues


WAMFT1

Recommended Posts

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>
Link to comment
Share on other sites

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)
  • Like 2
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.