How to get last login Access Date and time

Finally I found answer by myself. Let’s explore it with two user meta’s current_login, last_login.Lets see the code.

//function for setting the last login
function set_last_login($login) {
    $user = get_userdatabylogin($login);
    $curent_login_time = get_user_meta( $user->ID , 'current_login', true);
    //add or update the last login value for logged in user
    if(!empty($curent_login_time)){
        update_usermeta( $user->ID, 'last_login', $curent_login_time );
        update_usermeta( $user->ID, 'current_login', current_time('mysql') );
    }else {
        update_usermeta( $user->ID, 'current_login', current_time('mysql') );
        update_usermeta( $user->ID, 'last_login', current_time('mysql') );
    }
}

This will helps you to save the current and last login times. you can write an get last time function to get last login time. Here I wrote an article regarding the Last login and its working Kvcodes- How to set User Last Login Date and Time in WordPress Without Plugin

Leave a Comment