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.

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!

  • Can you specify the output value? An output in days could possibly result in something like 0.00752314814815 if the last action is only a few minutes ago...
    – Sven
    Dec 22 '13 at 20:31
  • @Sven It didn't occur to me that this may have been reason. I tried this using a user that would a value less than a day. But occurring to your comment, there should be a 0 and some numbers?
    – Dr.Hariri
    Dec 22 '13 at 20:44
  • @Sven Sorry for the last comment which was full of typos! Couldn't edit.
    – Dr.Hariri
    Dec 22 '13 at 21:29

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 );

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.

  • Thanks a lot Sven! This is working great! With regard to the last 2x set_user lines you wrote, would this be a good way to implement them? if( is_page() OR is_single() ) { set_user_last_active( get_current_user_id() ); set_user_last_active_days_ago( get_current_user_id() ); } else {} I am thinking of attaching this to wp_footer as add action to track the last activity of the user. Thanks a lot!
    – Dr.Hariri
    Dec 23 '13 at 15:47
  • This is the more final version I am planning to use: add_action( 'wp_footer', 'educadme_global_lastactive_update' ); function educadme_global_lastactive_update() { if( is_user_logged_in() ) { set_user_last_active( get_current_user_id() ); set_user_last_active_days_ago( get_current_user_id() ); } else {} }
    – Dr.Hariri
    Dec 23 '13 at 15:56
  • @A.Hariri Hey, I updated my answer with and an example on how to use it. Hope this will help you, but you're on the right track anyway :)
    – Sven
    Dec 23 '13 at 21:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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