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!
Okay, there are a couple of things to do:
last_active
for the userlast_active
last_active_days_ago
for the userSo you can go like this:
You’ll also have to set
last_active
somewhere:Note: You can also pass in a custom user ID within
set_user_last_active
andset_user_last_active_days_ago
instead of the current user ID.Update: You can call
set_user_last_active
andset_user_last_active_days_ago
within every action you’d like to. Let’s say you want to setlast_active
every time a user logged in to WordPress: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.