title: how to get last logged in admin time and date after login me in php
body: <p>I have 5 Admin in the database. So now I want to get the last user logged in date and time detail. Who worked before me.means after my logged in the dashboard I want to see previous USER login and logout time and date.</p>
  
  <p>I am able to get my (current user) login date and time. But I don't have an idea how can I get previous user(Admin) login and logout date/time detail.</p>
  
  <p>I am sending my login.php code.Where i wrote current user time/date code.Please see the code and give me suggestion how can i get previous user login and logout date/time.
  Thanks.</p>
  
  <pre><code>&lt;?php include "db.php"; ?&gt;
  &lt;?php session_start(); ?&gt;
  &lt;?php
  if(isset($_POST['login'])){
  $username = $_POST['username'];
  $password = $_POST['password'];
  
  $username = mysqli_real_escape_string($connection,$username);
  $password = mysqli_real_escape_string($connection,$password);
  $password = md5($password);
  $login_query = "SELECT * FROM users WHERE user_username = '$username'" ;
  $result_query = mysqli_query($connection,$login_query);
  $count = mysqli_num_rows($result_query);
  if(!$count){
  die("QUERY FAILED". mysqli_error($connection));
  }
  while($row= mysqli_fetch_array($result_query)){
  $log_user_id = $row['user_id'];
  $log_user_username = $row['user_username'];
  $log_user_password = $row['user_password'];
  $log_user_firstname = $row['user_firstname'];
  $log_user_lastname = $row['user_lastname'];
  $log_user_role = $row['user_role'];
  $log_user_time = $row['time'];
  $log_user_ip = $row['ip'];
  }
  if($username !== $log_user_username &amp;&amp; $password !== $log_user_password){
  header ("location: ../index.php");
  }elseif($username == $log_user_username &amp;&amp; $password == $log_user_password){
  if($count == 1) {
  $_SESSION['username']=$log_user_username;
  $_SESSION['firstname']=$log_user_firstname;
  $_SESSION['lastname']=$log_user_lastname;
  $_SESSION['user_role']=$log_user_role;
  $_SESSION['last_login'] = $log_user_time;
  $_SESSION['last_login_ip'] = $log_user_ip;
  
  date_default_timezone_set("Asia/Kolkata");
  $current_date = date("F d, Y, h:i:s A"); 
  $ip = $_SERVER['REMOTE_ADDR'];
  $query = "UPDATE users SET time= NOW() ,ip='$ip' WHERE user_username='$username'";
  mysqli_query($connection,$query);
  
  $last_login_date = $_SESSION['last_login'];
  $last_login_date2 = date('F d, Y, h:i:s A');
  $diffs = abs(strtotime($last_login_date2) - strtotime($last_login_date));
  $year = floor($diffs / (365*60*60*24));
  $month = floor(($diffs - $year * 365*60*60*24) / (30*60*60*24));
  $day = floor(($diffs - $year * 365*60*60*24 - $month*30*60*60*24)/ (60*60*24));
  $hour = floor(($diffs - $year * 365*60*60*24 - $month*30*60*60*24 - $day*60*60*24)/ (60*60)); 
  $minute = floor(($diffs - $year * 365*60*60*24 - $month*30*60*60*24 - $day*60*60*24 - $hour*60*60)/ 60); 
  $second = floor(($diffs - $year * 365*60*60*24 - $month*30*60*60*24 - $day*60*60*24 - $hour*60*60 - $minute*60));
  if($year == 0 AND $month == 0 AND $day == 0 AND $hour == 0 AND $minute == 0 AND $second &lt; 30 ) {
  $time1 = 'Just now';
  } elseif($year == 0 AND $month == 0 AND $day == 0 AND $hour == 0 AND $minute == 0 ) { 
  $time1 = 'few seconds ago';
  } elseif($year == 0 AND $month == 0 AND $day == 0 AND $hour == 0 AND $minute == 1) {
  $time1 = '1 minute ago';
  } elseif($year == 0 AND $month == 0 AND $day == 0 AND $hour == 0 ) {
  $time1 = $minute . ' minutes ago';
  } elseif($year == 0 AND $month == 0 AND $day == 0 AND $hour == 1 ) { 
  $time1 = '1 hour ago';
  } elseif($year == 0 AND $month == 0 AND $day == 0 ) {
  $time1 = $hour . ' hours ago';
  } elseif($year == 0 AND $month == 0 AND $day == 1 ) {
  $time1 = 'Yesterday';
  } elseif($year == 0 AND $month == 0 ) {
  $time1 = $day . ' days ago';
  } elseif($year == 0 AND $month == 1 ) {
  $time1 = '1 month ago';
  } elseif($year == 0) {
  $time1 = $month . ' months ago';
  } elseif($year == 1 ) {
  $time1 = '1 year ago';
  } else {
  $time1 = $year . ' years ago';
  } 
  }
  if($_SESSION['last_login_ip'] != $_SERVER['REMOTE_ADDR']) {
  $last_login_ip = "from this IP address (".$_SERVER['REMOTE_ADDR'].")";
  }
  else {
  $last_login_ip = "IP address ".$_SESSION['last_login_ip'];
  } 
  }
  header ("location: ../admin");
  }else{
  header ("location: ../index.php");
  }
  ?&gt;
  </code></pre>
  
  <p>So after my logged in on Dashboard (index.php) the page i did echo the variables and i can able to see current user login date/time detail.Which is my login detail.</p>
  
  <p>Please give me suggestion how can i get previous user login and logout detail.
  Thanks</p>
  
  <p>notification.php</p>
  
  <pre><code>   &lt;span class="pull-right text-muted small"&gt;&lt;em&gt;&lt;/em&gt;&lt;/span&gt;&lt;/a&gt;
  
  
                                 &lt;?php
  
  
  date_default_timezone_set("Asia/Kolkata");
  $last_login_date = date("F d, Y, h:i:s A");                               
  
  $last_login_date = $_SESSION['last_login'];
  $last_login_date2 = date('F d, Y, h:i:s A');
  $diffs = abs(strtotime($last_login_date2) - strtotime($last_login_date));
  $year = floor($diffs / (365*60*60*24));
  $month = floor(($diffs - $year * 365*60*60*24) / (30*60*60*24));
  $day = floor(($diffs - $year * 365*60*60*24 - $month*30*60*60*24)/ (60*60*24));
  $hour = floor(($diffs - $year * 365*60*60*24 - $month*30*60*60*24 - $day*60*60*24)/ (60*60)); 
  $minute = floor(($diffs - $year * 365*60*60*24 - $month*30*60*60*24 - $day*60*60*24 - $hour*60*60)/ 60); 
  $second = floor(($diffs - $year * 365*60*60*24 - $month*30*60*60*24 - $day*60*60*24 - $hour*60*60 - $minute*60));
  
  if($year == 0 AND $month == 0 AND $day == 0 AND $hour == 0 AND $minute == 0 AND $second &lt; 30 ) {
  $time1 = 'Just now';
  } elseif($year == 0 AND $month == 0 AND $day == 0 AND $hour == 0 AND $minute == 0 ) { 
  $time1 = 'few seconds ago';
  } elseif($year == 0 AND $month == 0 AND $day == 0 AND $hour == 0 AND $minute == 1) {
  $time1 = '1 minute ago';
  } elseif($year == 0 AND $month == 0 AND $day == 0 AND $hour == 0 ) {
  $time1 = $minute . ' minutes ago';
  } elseif($year == 0 AND $month == 0 AND $day == 0 AND $hour == 1 ) { 
  $time1 = '1 hour ago';
  } elseif($year == 0 AND $month == 0 AND $day == 0 ) {
  $time1 = $hour . ' hours ago';
  } elseif($year == 0 AND $month == 0 AND $day == 1 ) {
  $time1 = 'Yesterday';
  } elseif($year == 0 AND $month == 0 ) {
  $time1 = $day . ' days ago';
  } elseif($year == 0 AND $month == 1 ) {
  $time1 = '1 month ago';
  } elseif($year == 0) {
  $time1 = $month . ' months ago';
  } elseif($year == 1 ) {
  $time1 = '1 year ago';
  } else {
  $time1 = $year . ' years ago';
  
  } 
  
  if($_SESSION['last_login_ip'] != $_SERVER['REMOTE_ADDR']) {
      $last_login_ip = "From this IP address (".$_SERVER['REMOTE_ADDR'].")";
      }
      else {
      $last_login_ip = "IP address ".$_SESSION['last_login_ip'];
      }
  
  
  
  
  ?&gt;
  
  
                                    &lt;a href="#" class="list-group-item"&gt;
                                      &lt;i class="fa fa-user"&gt;&lt;/i&gt; User Name
                                      &lt;span class="pull-right text-muted small"&gt;&lt;em&gt;&lt;?php echo $_SESSION['username']; ?&gt;&lt;/em&gt;
                                      &lt;/span&gt;
                                  &lt;/a&gt;
  
                                  &lt;a href="#" class="list-group-item"&gt;
                                      &lt;i class="fa fa-sign-in"&gt;&lt;/i&gt; Last Login Time
                                      &lt;span class="pull-right text-muted small"&gt;&lt;em&gt;&lt;?php echo $time1; ?&gt;&lt;/em&gt;
                                      &lt;/span&gt;
                                  &lt;/a&gt;
                                  &lt;a href="#" class="list-group-item"&gt;
                                      &lt;i class="fa fa-sign-out"&gt;&lt;/i&gt; Last Logout Time
                                      &lt;span class="pull-right text-muted small"&gt;&lt;em&gt;27 minutes ago&lt;/em&gt;
                                      &lt;/span&gt;
                                  &lt;/a&gt;
                                  &lt;a href="#" class="list-group-item"&gt;
                                      &lt;i class="fa fa-tasks fa-fw"&gt;&lt;/i&gt; User Ip
                                      &lt;span class="pull-right text-muted small"&gt;&lt;em&gt;&lt;?php echo $last_login_ip; ?&gt;&lt;/em&gt;
                                      &lt;/span&gt;
                                  &lt;/a&gt;
  </code></pre>
  
  <p>here iam getting admin login date and time detail.But i unable to get previous visitor login detail . i can just see only my login time/date detail after login in here.
  So please help.</p>