Last Login in number of days format

I am trying to capture last user active date and reproduce that into another user-meta field in days only without any text with it. Being very bad with coding, I was hoping someone could help me shed some light on how best to achieve this.

I checked many other solutions in the forum but non worked the way I wanted.

Read More

Currently I am using this:

function set_user_last_active( $user_id ) {
$user = get_userdata ( $user_id );

$dateoflast = strtotime( get_user_meta($user_id, 'last_active') );             
$datenow = strtotime( current_time('mysql') );
$difference = abs($datenow - $dateoflast / 86400);

//add or update the last login value for logged in user
update_usermeta( $user->ID, 'last_active', current_time('mysql') );
update_usermeta( $user->ID, 'last_active_days_ago', human_time_diff( strtotime( get_user_meta($user_id, 'last_active', true) ) ) );
update_usermeta( $user->ID, 'last_active_days_ago2', $difference);                              
}

As you can see, I am trying different ways to achieve this but the closest I got to is the second update_usermeta which outputs a number along with minutes.

Although I took the third one from a post here, it outputs a long number that keeps increasing: 1387740860

I hope I can find someone who tried a similar thing or experienced enough to help me do it.

Thanks a lot!

Related posts

1 comment

  1. Okay, there are a couple of things to do:

    • Get last_active for the user
    • Calculate the days since last_active
    • Set last_active_days_ago for the user

    So you can go like this:

    function daysAgo( $time ) {
      $time = time() - $time;
      $daysAgo = $time / 86400; // calculate days
        return $daysAgo;
    }
    
    function set_user_last_active_days_ago( $user_id ) {
      // get last time active
      $time = get_user_meta( get_current_user_id(), 'last_active', true );
    
      if ( !empty($time) ) :
        // calculate time since last_active
        $time = daysAgo( strtotime($time) );
        // update user meta
        update_user_meta( $user_id, 'last_active_days_ago', $time );
      endif;
    }
    
    set_user_last_active_days_ago( get_current_user_id() );
    

    You’ll also have to set last_active somewhere:

    function set_user_last_active( $user_id ) {
      update_user_meta( $user_id, 'last_active', date('Y-m-d H:i:s') );
    }
    
    set_user_last_active( get_current_user_id() );
    

    Note: You can also pass in a custom user ID within set_user_last_active and set_user_last_active_days_ago instead of the current user ID.

    Update: You can call set_user_last_active and set_user_last_active_days_ago within every action you’d like to. Let’s say you want to set last_active every time a user logged in to WordPress:

    add_action('wp_login', 'user_login_last_active', 10, 2);
    function user_login_last_active($user_login, $user) {
      set_user_last_active( $user->ID );
    }
    

    It’s completely up to you when you want to set the data. It can be set per login (like above) or e.g. every time a post is saved or the profile is updated.

    If none of these actions will fit your need you can take a look the available Actions at the Codex.

Comments are closed.