I use this function to get the last time a user logged in.

function jkn_last_login($user_login) {
    $user = get_user_by( 'login', $user_login );
    global $last_login;
    $last_login = array();
    $last_login[$user->ID] = get_user_meta($user->ID,'last-login',true);
    update_user_meta( $user->ID, 'last-login', time() );

As you can see i define a global $last_login to save the last login time.

But if i try to echo the value of $last_login nothing happen.

global $last_login;
global $current_user;
echo $last_login[$current_user->ID];

2 Answers 2


Don't use global variables. You can do it like this without global variable:

global $current_user;
echo get_user_meta( $current_user->ID, 'last-login', true );

Not sure if you can use the global keyword twice?


global $current_user, $last_login; echo $last_login[$current_user->ID];

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.