Home / date / Capture users last login date and time
  • How to Capture users last login date and time

    capture-users-last-login-date-and-time

    Adding the first snippet to the functions.php of your wordpress theme will capture a users last login date. Recently I updated the script for 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 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.

    ( click code to copy )

    wordpress snippet : PHP - Current Version

    <>
    // set the 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' );
        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 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' );
      }else{
        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.

    ( click code to copy )

    wordpress snippet : PHP

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

    ( click code to copy )

    wordpress snippet : PHP

    <>
      // how many days since last login
      global $current_user;
      get_currentuserinfo();
      $now = time();
      $last_login = wpsnipp_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 snippet as it is the most current version using get_user_by().

    ( click code to copy )

    wordpress snippet : PHP - Deprecated

    <>
    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;
    }
    
    ( click code to copy )

    wordpress snippet : PHP - Deprecated

    <>
    <?php
             global $userdata;
             get_currentuserinfo();
             echo  'You last logged in:';
             get_last_login($userdata->ID);
    ?>
    
  • Lars

    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

    • 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.

  • MatthewJ

    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 😉

    • 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/

      • MatthewJ

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

      • Lars

        Thanks Kevin,

        Will test that.
        Take care / Lars

  • Danielconde Uy

    Deprecated