X

How to Capture Users Last Login Date and Time in WordPress

Snippets by IsItWP

Are you looking for a way to capture a user’s last login date and time? While there’s probably a plugin for this, we have created a quick code snippet that you can use to capture users last login date and time in WordPress.

Instructions:

All you have to do is add this code to your theme’s functions.php file or in a site-specific plugin:


// set the last login date
add_action('wp_login','iiwp_set_last_login', 0, 2);
function iiwp_set_last_login($login, $user) {  
    $user = get_user_by('login',$login);
    $time = current_time( 'timestamp' );
    $last_login = get_user_meta( $user->ID, '_last_login', 'true' );

    if(!$last_login){
    update_usermeta( $user->ID, '_last_login', $time );
    }else{
    update_usermeta( $user->ID, '_last_login_prev', $last_login );
    update_usermeta( $user->ID, '_last_login', $time );
    }

}

// get last login date
function iiwp_get_last_login($user_id,$prev=null){

  $last_login = get_user_meta($user_id);
  $time = current_time( 'timestamp' );

  if(isset($last_login['_last_login_prev'][0]) && $prev){
          $last_login = get_user_meta($user_id, '_last_login_prev', 'true' );
  }else if(isset($last_login['_last_login'][0])){
          $last_login = get_user_meta($user_id, '_last_login', 'true' );
  }else{
    update_usermeta( $user_id, '_last_login', $time );
    $last_login = $last_login['_last_login'][0];
  }

  return $last_login;
}

You can then use the following code to display the last login date. You may also want to show the previous login date to your users. Simply use iiwp_get_last_login($current_user->ID,true)) to get the previous login date not the current.

  // show last login date
  global $current_user;
  get_currentuserinfo();
  echo '<p>Previous: Login date: ' . date("Y-m-d h:m:s", iiwp_get_last_login($current_user->ID,true)) . '</p>';
  echo '<p>Current: Login date: ' . date("Y-m-d h:m:s", iiwp_get_last_login($current_user->ID)) . '</p>';

You can also compare the last login date to the current date and get the days since last login with following code.

  // how many days since last login
  global $current_user;
  get_currentuserinfo();

  $now = time();
  $last_login = iiwp_get_last_login($current_user->ID,true);
  $datediff = $now - $last_login;

  echo '<p>Days since last login: ' . floor($datediff/(60*60*24)) . '</p>';


Some functions in the old version below have been deprecated including get_userdatabylogin(). Please use the above code as it is the most current version using get_user_by().

function your_last_login($login) {
    global $user_ID;
    $user = get_userdatabylogin($login);
    update_usermeta($user->ID, 'last_login', current_time('mysql'));
}
add_action('wp_login','your_last_login');

function get_last_login($user_id) {
    $last_login = get_user_meta($user_id, 'last_login', true);
    $date_format = get_option('date_format') . ' ' . get_option('time_format');
    $the_last_login = mysql2date($date_format, $last_login, false);
    echo $the_last_login;
}
<?php
         global $userdata;
         get_currentuserinfo();
         echo  'You last logged in:'; 
         get_last_login($userdata->ID); 
?>

Note: If this is your first time adding code snippets in WordPress, then please refer to our guide on how to properly add code snippets in WordPress, so you don’t accidentally break your site.

If you liked this code snippet, please consider checking out our other articles on the site like: 23 effective tips to get more email subscribers today and how to create a WordPress form with a date picker

Comments   Leave a Reply

  1. how to show last login time and date of all users as a list

  2. in show last login date time format is wrong. please correct it. h:i:s is correct

  3. Great sharing. Really helpful and informative article. Thanks for sharing with us.

  4. Web design Cheshire May 12, 2017 at 8:24 am

    Really handy code here. Thank you.

  5. Toque de Silencio May 5, 2017 at 6:10 am

    Hi,
    I´m new in snippets, so how show I this (or any) snippet in frontend?

  6. Danielconde Uy March 20, 2012 at 1:51 pm

    Deprecated

  7. Hi! Great code, thanks a lot! I have a question though: would it be possible to have a list of all the users showing if they’re connected and the last time they logged in if they’re not?

    I think it would be a very interesting information for any admin working with multiple contributors.

    Thanks again 😉

    1. Thanks glad you like the snippet. This could be done but to that extent I think a plugin would be best suited a few plugins are available.

      http://wordpress.org/extend/plugins/who-is-online/

      1. Looks like a great plugin although a little outdated but I will try it, thanks!

      2. Thanks Kevin,

        Will test that.
        Take care / Lars

  8. Great, I shall test it.

    BUT, do you know where to find the code for being able to see how many times each user login to wordpress and also the last date. (MySQL database to being able to get the lost out?
    Regards / Lars

    1. Well the above will handle the last login date, however the number of times a user has logged in could be done. Ill have to see about adding that as a snippet I looked but could not find anything.

Add a Comment

We're glad you have chosen to leave a comment. Please keep in mind that all comments are moderated according to our privacy policy, and all links are nofollow. Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.

WordPress Launch Checklist

The Ultimate WordPress Launch Checklist

We've compiled all the essential checklist items for your next WordPress website launch into one handy ebook.
Yes, Send Me the Free eBook!