Adding the first snippet to the functions.php of your WordPress theme will capture a users last login date. Recently I updated the script to the most current version of WordPress. Things have been changed to wpsnipp_get_last_login() and wpsnipp_set_last_login() should be obvious what they do. One will update the users last login data in the user meta and the other will let you get the last date so you can display it on your site. We also save the current date and also the previous date so you can choose what you display to admins vs subscribers.

users last login
Wondering how active a certain user is or how long it’s been since he last logged in? Perhaps you need to monitor when your writers or editors last checked into the site.
// set the users last login date
add_action('wp_login','wpsnipp_set_last_login', 0, 2);
function wpsnipp_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' );
    update_usermeta( $user->ID, '_last_login', $time );
    update_usermeta( $user->ID, '_last_login_prev', $last_login );
    update_usermeta( $user->ID, '_last_login', $time );
// get last users login date
function wpsnipp_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' );
    update_usermeta( $user_id, '_last_login', $time );
    $last_login = $last_login['_last_login'][0];
  return $last_login;

You can then use the following to display the last login date. You may also want to show the previous login date to your users. I save that to user meta in the snippet above. wpsnipp_get_last_login($current_user->ID,true)) adding true will get the previous login date not the current.

 // show last login date
  global $current_user;
  echo '

Previous: Login date: ' . date("Y-m-d h:m:s", wpsnipp_get_last_login($current_user->ID,true)) . '

'; echo '

Current: Login date: ' . date("Y-m-d h:m:s", wpsnipp_get_last_login($current_user->ID)) . '


You can also compare the last login date to the current date and get the days since the users last login.

 // how many days since last login
  global $current_user;
  $now = time();
  $last_login = wpsnipp_get_last_login($current_user->ID,true);
  $datediff = $now - $last_login;
  echo '

Days since last login: ' . floor($datediff/(60*60*24)) . '


Some functions in the old version below have been deprecated including get_userdatabylogin(). Please use the above snippet 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'));
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;
         echo  'You last logged in:';
Welcome to Wbcom Designs!  - Get 10% off on all plugins & themes
Coupon : HELLO10